quantcrypt/kem/api/
algorithm.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use crate::kem::common::{config::oids::Oid, kem_type::KemType};
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter};

#[derive(Clone, Debug, PartialEq, EnumIter, Display, Copy)]
/// The permissible algorithms for the `AlgorithmIdentifier` type.
pub enum KemAlgorithm {
    /// Pure KEMs
    MlKem512,
    MlKem768,
    MlKem1024,

    // The composite algorithm list from the old version
    MlKem512P256,
    MlKem512BrainpoolP256r1,
    MlKem512X25519,
    MlKem512Rsa2048,
    MlKem512Rsa3072,
    MlKem768P256,

    // The compsite algorithm list is from the latest editor's draft:
    //https://lamps-wg.github.io/draft-composite-kem/draft-ietf-lamps-pq-composite-kem.html
    MlKem768Rsa2048,
    MlKem768Rsa3072,
    MlKem768Rsa4096,
    MlKem768X25519,
    MlKem768P384,
    MlKem768BrainpoolP256r1,
    MlKem1024P384,
    MlKem1024BrainpoolP384r1,
    MlKem1024X448,
}

impl KemAlgorithm {
    /// Get all KEM algorithms
    pub(crate) fn all() -> Vec<KemAlgorithm> {
        KemAlgorithm::iter().collect()
    }

    /// Get the corresponding `DsaType` for the algorithm
    pub(crate) fn get_kem_type(&self) -> KemType {
        match self {
            // Pure KEMs
            KemAlgorithm::MlKem512 => KemType::MlKem512,
            KemAlgorithm::MlKem768 => KemType::MlKem768,
            KemAlgorithm::MlKem1024 => KemType::MlKem1024,

            // The composite algorithm list from the old version
            KemAlgorithm::MlKem512P256 => KemType::MlKem512P256,
            KemAlgorithm::MlKem512BrainpoolP256r1 => KemType::MlKem512BrainpoolP256r1,
            KemAlgorithm::MlKem512X25519 => KemType::MlKem512X25519,
            KemAlgorithm::MlKem512Rsa2048 => KemType::MlKem512Rsa2048,
            KemAlgorithm::MlKem512Rsa3072 => KemType::MlKem512Rsa3072,
            KemAlgorithm::MlKem768P256 => KemType::MlKem768P256,

            // The compsite algorithm list is from the latest editor's draft:
            //https://lamps-wg.github.io/draft-composite-kem/draft-ietf-lamps-pq-composite-kem.html
            KemAlgorithm::MlKem768Rsa2048 => KemType::MlKem768Rsa2048,
            KemAlgorithm::MlKem768Rsa3072 => KemType::MlKem768Rsa3072,
            KemAlgorithm::MlKem768Rsa4096 => KemType::MlKem768Rsa4096,
            KemAlgorithm::MlKem768X25519 => KemType::MlKem768X25519,
            KemAlgorithm::MlKem768P384 => KemType::MlKem768P384,
            KemAlgorithm::MlKem768BrainpoolP256r1 => KemType::MlKem768BrainpoolP256r1,
            KemAlgorithm::MlKem1024P384 => KemType::MlKem1024P384,
            KemAlgorithm::MlKem1024BrainpoolP384r1 => KemType::MlKem1024BrainpoolP384r1,
            KemAlgorithm::MlKem1024X448 => KemType::MlKem1024X448,
        }
    }

    /// Check if the algorithm is a composite or pure algorithm
    ///
    /// # Returns
    ///
    /// True if the algorithm is a composite algorithm, false otherwise
    pub fn is_composite(&self) -> bool {
        !matches!(
            self,
            KemAlgorithm::MlKem512 | KemAlgorithm::MlKem768 | KemAlgorithm::MlKem1024
        )
    }

    /// Get the OID for the algorithm
    ///
    /// # Returns
    ///
    /// The OID for the algorithm
    pub fn get_oid(&self) -> String {
        self.get_kem_type().get_oid()
    }

    /// Get the KEM algorithm from an OID
    ///
    /// # Arguments
    ///
    /// * `oid` - The OID of the KEM algorithm
    ///
    /// # Returns
    ///
    /// The KEM algorithm corresponding to the OID, or None if the OID is not recognized
    pub fn from_oid(oid: &str) -> Option<KemAlgorithm> {
        KemAlgorithm::all()
            .iter()
            .find(|x| x.get_oid() == oid)
            .cloned()
    }
}