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