Skip to main content

rtci_sframe/
cipher_suite.rs

1use std::fmt;
2use std::str::FromStr;
3
4use crypto::{AeadAlgorithm, HmacAlgorithm, StreamCipherAlgorithm};
5
6/// The size in bytes of every SFrame nonce (`AEAD.Nn`): 12 for all registered suites.
7pub const NONCE_LEN: usize = 12;
8
9/// An SFrame cipher suite: the hash used for key derivation and the AEAD used for the frames.
10///
11/// The first five are the suites of RFC 9605 section 4.5. The three `Aes256CtrHmacSha512`
12/// suites are the AES-256 counterparts of the AES-CTR construction (IANA "SFrame Cipher Suites"
13/// values 0x0006 to 0x0008, from draft-barnes-sframe-iana-256), which the W3C Encoded Transform
14/// API lists in `SFrameCipherSuite`.
15#[non_exhaustive]
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub enum CipherSuite {
18    /// `AES_128_CTR_HMAC_SHA256_80` (0x0001): AES-128-CTR with HMAC-SHA256, 80-bit tag.
19    Aes128CtrHmacSha256_80,
20    /// `AES_128_CTR_HMAC_SHA256_64` (0x0002): AES-128-CTR with HMAC-SHA256, 64-bit tag.
21    Aes128CtrHmacSha256_64,
22    /// `AES_128_CTR_HMAC_SHA256_32` (0x0003): AES-128-CTR with HMAC-SHA256, 32-bit tag.
23    Aes128CtrHmacSha256_32,
24    /// `AES_128_GCM_SHA256_128` (0x0004): AES-128-GCM, keys derived with SHA-256.
25    Aes128GcmSha256_128,
26    /// `AES_256_GCM_SHA512_128` (0x0005): AES-256-GCM, keys derived with SHA-512.
27    Aes256GcmSha512_128,
28    /// `AES_256_CTR_HMAC_SHA512_80` (0x0006): AES-256-CTR with HMAC-SHA512, 80-bit tag.
29    Aes256CtrHmacSha512_80,
30    /// `AES_256_CTR_HMAC_SHA512_64` (0x0007): AES-256-CTR with HMAC-SHA512, 64-bit tag.
31    Aes256CtrHmacSha512_64,
32    /// `AES_256_CTR_HMAC_SHA512_32` (0x0008): AES-256-CTR with HMAC-SHA512, 32-bit tag.
33    Aes256CtrHmacSha512_32,
34}
35
36/// How a suite encrypts: a native AEAD, or the AES-CTR with HMAC construction of RFC 9605
37/// section 4.5.1.
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub(crate) enum AeadKind {
40    Gcm(AeadAlgorithm),
41    CtrHmac(StreamCipherAlgorithm),
42}
43
44impl CipherSuite {
45    /// Every suite this crate implements, in registry order.
46    pub const ALL: [CipherSuite; 8] = [
47        Self::Aes128CtrHmacSha256_80,
48        Self::Aes128CtrHmacSha256_64,
49        Self::Aes128CtrHmacSha256_32,
50        Self::Aes128GcmSha256_128,
51        Self::Aes256GcmSha512_128,
52        Self::Aes256CtrHmacSha512_80,
53        Self::Aes256CtrHmacSha512_64,
54        Self::Aes256CtrHmacSha512_32,
55    ];
56
57    /// The suite's value in the IANA "SFrame Cipher Suites" registry (RFC 9605 section 8.1).
58    /// It is part of the key derivation labels.
59    #[must_use]
60    pub const fn id(self) -> u16 {
61        match self {
62            Self::Aes128CtrHmacSha256_80 => 0x0001,
63            Self::Aes128CtrHmacSha256_64 => 0x0002,
64            Self::Aes128CtrHmacSha256_32 => 0x0003,
65            Self::Aes128GcmSha256_128 => 0x0004,
66            Self::Aes256GcmSha512_128 => 0x0005,
67            Self::Aes256CtrHmacSha512_80 => 0x0006,
68            Self::Aes256CtrHmacSha512_64 => 0x0007,
69            Self::Aes256CtrHmacSha512_32 => 0x0008,
70        }
71    }
72
73    /// The suite with this registry value, if this crate implements it. 0x0000 is reserved and
74    /// 0xF000 to 0xFFFF are for private use: neither names a suite.
75    #[must_use]
76    pub const fn from_id(id: u16) -> Option<Self> {
77        Some(match id {
78            0x0001 => Self::Aes128CtrHmacSha256_80,
79            0x0002 => Self::Aes128CtrHmacSha256_64,
80            0x0003 => Self::Aes128CtrHmacSha256_32,
81            0x0004 => Self::Aes128GcmSha256_128,
82            0x0005 => Self::Aes256GcmSha512_128,
83            0x0006 => Self::Aes256CtrHmacSha512_80,
84            0x0007 => Self::Aes256CtrHmacSha512_64,
85            0x0008 => Self::Aes256CtrHmacSha512_32,
86            _ => return None,
87        })
88    }
89
90    /// The suite's registered name, which is also its `SFrameCipherSuite` value in the W3C
91    /// Encoded Transform API.
92    #[must_use]
93    pub const fn name(self) -> &'static str {
94        match self {
95            Self::Aes128CtrHmacSha256_80 => "AES_128_CTR_HMAC_SHA256_80",
96            Self::Aes128CtrHmacSha256_64 => "AES_128_CTR_HMAC_SHA256_64",
97            Self::Aes128CtrHmacSha256_32 => "AES_128_CTR_HMAC_SHA256_32",
98            Self::Aes128GcmSha256_128 => "AES_128_GCM_SHA256_128",
99            Self::Aes256GcmSha512_128 => "AES_256_GCM_SHA512_128",
100            Self::Aes256CtrHmacSha512_80 => "AES_256_CTR_HMAC_SHA512_80",
101            Self::Aes256CtrHmacSha512_64 => "AES_256_CTR_HMAC_SHA512_64",
102            Self::Aes256CtrHmacSha512_32 => "AES_256_CTR_HMAC_SHA512_32",
103        }
104    }
105
106    /// `Hash.Nh`: the output size in bytes of the suite's hash function.
107    #[must_use]
108    pub const fn hash_len(self) -> usize {
109        match self {
110            Self::Aes128CtrHmacSha256_80
111            | Self::Aes128CtrHmacSha256_64
112            | Self::Aes128CtrHmacSha256_32
113            | Self::Aes128GcmSha256_128 => 32,
114            Self::Aes256GcmSha512_128
115            | Self::Aes256CtrHmacSha512_80
116            | Self::Aes256CtrHmacSha512_64
117            | Self::Aes256CtrHmacSha512_32 => 64,
118        }
119    }
120
121    /// `AEAD.Nka`: the AES key size in bytes of a suite built on AES-CTR with HMAC; `None` for
122    /// the GCM suites.
123    #[must_use]
124    pub const fn aes_ctr_key_len(self) -> Option<usize> {
125        match self {
126            Self::Aes128CtrHmacSha256_80
127            | Self::Aes128CtrHmacSha256_64
128            | Self::Aes128CtrHmacSha256_32 => Some(16),
129            Self::Aes256CtrHmacSha512_80
130            | Self::Aes256CtrHmacSha512_64
131            | Self::Aes256CtrHmacSha512_32 => Some(32),
132            Self::Aes128GcmSha256_128 | Self::Aes256GcmSha512_128 => None,
133        }
134    }
135
136    /// `AEAD.Nk`: the size in bytes of the derived `sframe_key`. For the AES-CTR suites it is
137    /// `Nka + Nh` (an encryption subkey and an authentication subkey). It is also the length an
138    /// MLS exporter is asked for (RFC 9605 section 5.2).
139    #[must_use]
140    pub const fn key_len(self) -> usize {
141        match self {
142            Self::Aes128GcmSha256_128 => 16,
143            Self::Aes256GcmSha512_128 => 32,
144            Self::Aes128CtrHmacSha256_80
145            | Self::Aes128CtrHmacSha256_64
146            | Self::Aes128CtrHmacSha256_32 => 16 + 32,
147            Self::Aes256CtrHmacSha512_80
148            | Self::Aes256CtrHmacSha512_64
149            | Self::Aes256CtrHmacSha512_32 => 32 + 64,
150        }
151    }
152
153    /// `AEAD.Nn`: the nonce size in bytes.
154    #[must_use]
155    pub const fn nonce_len(self) -> usize {
156        NONCE_LEN
157    }
158
159    /// `AEAD.Nt`: the size in bytes of the authentication tag the suite appends.
160    #[must_use]
161    pub const fn tag_len(self) -> usize {
162        match self {
163            Self::Aes128CtrHmacSha256_80 | Self::Aes256CtrHmacSha512_80 => 10,
164            Self::Aes128CtrHmacSha256_64 | Self::Aes256CtrHmacSha512_64 => 8,
165            Self::Aes128CtrHmacSha256_32 | Self::Aes256CtrHmacSha512_32 => 4,
166            Self::Aes128GcmSha256_128 | Self::Aes256GcmSha512_128 => 16,
167        }
168    }
169
170    /// The HMAC that HKDF runs on, and that the AES-CTR suites authenticate with.
171    pub(crate) const fn hmac(self) -> HmacAlgorithm {
172        match self.hash_len() {
173            32 => HmacAlgorithm::Sha256,
174            _ => HmacAlgorithm::Sha512,
175        }
176    }
177
178    pub(crate) const fn aead_kind(self) -> AeadKind {
179        match self {
180            Self::Aes128GcmSha256_128 => AeadKind::Gcm(AeadAlgorithm::Aes128Gcm),
181            Self::Aes256GcmSha512_128 => AeadKind::Gcm(AeadAlgorithm::Aes256Gcm),
182            Self::Aes128CtrHmacSha256_80
183            | Self::Aes128CtrHmacSha256_64
184            | Self::Aes128CtrHmacSha256_32 => AeadKind::CtrHmac(StreamCipherAlgorithm::Aes128Ctr),
185            Self::Aes256CtrHmacSha512_80
186            | Self::Aes256CtrHmacSha512_64
187            | Self::Aes256CtrHmacSha512_32 => AeadKind::CtrHmac(StreamCipherAlgorithm::Aes256Ctr),
188        }
189    }
190}
191
192impl fmt::Display for CipherSuite {
193    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
194        formatter.write_str(self.name())
195    }
196}
197
198/// The name is not that of a cipher suite this crate implements.
199#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
200#[error("unknown SFrame cipher suite name")]
201pub struct UnknownCipherSuite;
202
203impl FromStr for CipherSuite {
204    type Err = UnknownCipherSuite;
205
206    /// Parses a registered name, exactly as registered.
207    fn from_str(name: &str) -> Result<Self, Self::Err> {
208        Self::ALL
209            .into_iter()
210            .find(|suite| suite.name() == name)
211            .ok_or(UnknownCipherSuite)
212    }
213}
214
215impl TryFrom<u16> for CipherSuite {
216    type Error = UnknownCipherSuite;
217
218    fn try_from(id: u16) -> Result<Self, Self::Error> {
219        Self::from_id(id).ok_or(UnknownCipherSuite)
220    }
221}
222
223impl From<CipherSuite> for u16 {
224    fn from(suite: CipherSuite) -> Self {
225        suite.id()
226    }
227}