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]
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18pub enum CipherSuite {
19 Aes128CtrHmacSha256_80,
21 Aes128CtrHmacSha256_64,
23 Aes128CtrHmacSha256_32,
25 Aes128GcmSha256_128,
27 Aes256GcmSha512_128,
29 Aes256CtrHmacSha512_80,
31 Aes256CtrHmacSha512_64,
33 Aes256CtrHmacSha512_32,
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40pub(crate) enum AeadKind {
41 Gcm(AeadAlgorithm),
42 CtrHmac(StreamCipherAlgorithm),
43}
44
45impl CipherSuite {
46 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 #[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 #[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 #[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 #[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 #[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 #[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 #[must_use]
156 pub const fn nonce_len(self) -> usize {
157 NONCE_LEN
158 }
159
160 #[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 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#[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 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}