Skip to main content

core_crypto/mls/
cipher_suite.rs

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