rtci_sframe/
cipher_suite.rs1use std::fmt;
2use std::str::FromStr;
3
4use crypto::{AeadAlgorithm, HmacAlgorithm, StreamCipherAlgorithm};
5
6pub const NONCE_LEN: usize = 12;
8
9#[non_exhaustive]
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub enum CipherSuite {
18 Aes128CtrHmacSha256_80,
20 Aes128CtrHmacSha256_64,
22 Aes128CtrHmacSha256_32,
24 Aes128GcmSha256_128,
26 Aes256GcmSha512_128,
28 Aes256CtrHmacSha512_80,
30 Aes256CtrHmacSha512_64,
32 Aes256CtrHmacSha512_32,
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub(crate) enum AeadKind {
40 Gcm(AeadAlgorithm),
41 CtrHmac(StreamCipherAlgorithm),
42}
43
44impl CipherSuite {
45 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 #[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 #[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 #[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 #[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 #[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 #[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 #[must_use]
155 pub const fn nonce_len(self) -> usize {
156 NONCE_LEN
157 }
158
159 #[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 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#[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 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}