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
use crate::prelude::CiphersuiteName;
use crate::{CryptoError, CryptoResult};
use openmls_traits::types::{Ciphersuite, HashType};
use wire_e2e_identity::prelude::HashAlgorithm;

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, derive_more::Deref, serde::Serialize, serde::Deserialize)]
#[serde(transparent)]
#[repr(transparent)]
/// A wrapper for the OpenMLS Ciphersuite, so that we are able to provide a default value.
pub struct MlsCiphersuite(pub(crate) Ciphersuite);

impl MlsCiphersuite {
    pub(crate) fn e2ei_hash_alg(&self) -> HashAlgorithm {
        match self.0.hash_algorithm() {
            HashType::Sha2_256 => HashAlgorithm::SHA256,
            HashType::Sha2_384 => HashAlgorithm::SHA384,
            HashType::Sha2_512 => HashAlgorithm::SHA512,
        }
    }
}

impl Default for MlsCiphersuite {
    fn default() -> Self {
        Self(Ciphersuite::MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519)
    }
}

impl From<Ciphersuite> for MlsCiphersuite {
    fn from(value: Ciphersuite) -> Self {
        Self(value)
    }
}

impl From<MlsCiphersuite> for Ciphersuite {
    fn from(ciphersuite: MlsCiphersuite) -> Self {
        ciphersuite.0
    }
}

impl From<MlsCiphersuite> for u16 {
    fn from(cs: MlsCiphersuite) -> Self {
        (&cs.0).into()
    }
}

impl TryFrom<u16> for MlsCiphersuite {
    type Error = CryptoError;

    fn try_from(c: u16) -> CryptoResult<Self> {
        Ok(CiphersuiteName::try_from(c)
            .map_err(|_| CryptoError::ImplementationError)?
            .into())
    }
}