Skip to main content

core_crypto/mls/
cipher_suite.rs

1use openmls_traits::types::HashType;
2use wire_e2e_identity::HashAlgorithm;
3
4use crate::MlsCiphersuite;
5
6/// The cipher suite identifier presented does not map to a known ciphersuite.
7#[derive(Debug, thiserror::Error)]
8#[error("Unknown cipher suite")]
9pub struct UnknownCipherSuite;
10
11#[derive(
12    Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, derive_more::Deref, serde::Serialize, serde::Deserialize,
13)]
14#[serde(transparent)]
15#[repr(transparent)]
16/// A wrapper for the OpenMLS Ciphersuite, so that we are able to provide a default value.
17pub struct CipherSuite(pub(crate) MlsCiphersuite);
18
19impl CipherSuite {
20    pub(crate) fn e2ei_hash_alg(&self) -> HashAlgorithm {
21        match self.0.hash_algorithm() {
22            HashType::Sha2_256 => HashAlgorithm::SHA256,
23            HashType::Sha2_384 => HashAlgorithm::SHA384,
24            HashType::Sha2_512 => HashAlgorithm::SHA512,
25        }
26    }
27}
28
29impl Default for CipherSuite {
30    fn default() -> Self {
31        Self(MlsCiphersuite::MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519)
32    }
33}
34
35impl From<MlsCiphersuite> for CipherSuite {
36    fn from(value: MlsCiphersuite) -> Self {
37        Self(value)
38    }
39}
40
41impl From<CipherSuite> for MlsCiphersuite {
42    fn from(cipher_suite: CipherSuite) -> Self {
43        cipher_suite.0
44    }
45}
46
47impl From<CipherSuite> for u16 {
48    fn from(cs: CipherSuite) -> Self {
49        (&cs.0).into()
50    }
51}
52
53impl TryFrom<u16> for CipherSuite {
54    type Error = UnknownCipherSuite;
55
56    fn try_from(c: u16) -> Result<Self, UnknownCipherSuite> {
57        Ok(MlsCiphersuite::try_from(c).map_err(|_| UnknownCipherSuite)?.into())
58    }
59}
60
61impl PartialEq<MlsCiphersuite> for CipherSuite {
62    fn eq(&self, other: &MlsCiphersuite) -> bool {
63        self.0 == *other
64    }
65}