1pub mod access_rights;
17pub mod agreement;
18pub mod gost3410;
19pub mod hls;
20pub mod signature;
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum SecuritySuite {
25 Suite0,
27 Suite1,
29 Suite2,
31}
32
33impl SecuritySuite {
34 pub fn id(&self) -> u8 {
36 match self {
37 SecuritySuite::Suite0 => 0,
38 SecuritySuite::Suite1 => 1,
39 SecuritySuite::Suite2 => 2,
40 }
41 }
42
43 pub fn from_id(id: u8) -> Option<SecuritySuite> {
45 match id {
46 0 => Some(SecuritySuite::Suite0),
47 1 => Some(SecuritySuite::Suite1),
48 2 => Some(SecuritySuite::Suite2),
49 _ => None,
50 }
51 }
52
53 pub fn name(&self) -> &'static str {
55 match self {
56 SecuritySuite::Suite0 => "AES-GCM-128",
57 SecuritySuite::Suite1 => "ECDH-ECDSA-AES-GCM-128-SHA-256",
58 SecuritySuite::Suite2 => "ECDH-ECDSA-AES-GCM-256-SHA-384",
59 }
60 }
61
62 pub fn aes_key_len(&self) -> usize {
64 match self {
65 SecuritySuite::Suite0 | SecuritySuite::Suite1 => 16,
66 SecuritySuite::Suite2 => 32,
67 }
68 }
69
70 pub fn has_public_key(&self) -> bool {
72 !matches!(self, SecuritySuite::Suite0)
73 }
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq)]
79pub enum SecurityPolicy {
80 None,
82 Authentication,
84 Encryption,
86 AuthenticationEncryption,
88}
89
90impl SecurityPolicy {
91 pub fn security_control_bits(&self) -> u8 {
94 match self {
95 SecurityPolicy::None => 0x00,
96 SecurityPolicy::Authentication => 0x10,
97 SecurityPolicy::Encryption => 0x20,
98 SecurityPolicy::AuthenticationEncryption => 0x30,
99 }
100 }
101
102 pub fn from_security_control(sc: u8) -> SecurityPolicy {
104 match (sc & 0x10 != 0, sc & 0x20 != 0) {
105 (false, false) => SecurityPolicy::None,
106 (true, false) => SecurityPolicy::Authentication,
107 (false, true) => SecurityPolicy::Encryption,
108 (true, true) => SecurityPolicy::AuthenticationEncryption,
109 }
110 }
111
112 pub fn security_control_byte(&self, suite: SecuritySuite) -> u8 {
114 self.security_control_bits() | (suite.id() & 0x0F)
115 }
116}
117
118#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
123pub enum AuthMechanism {
124 None,
126 Lls,
128 HlsManufacturer,
130 HlsMd5,
132 HlsSha1,
134 HlsGmac,
136 HlsSha256,
138 HlsEcdsa,
140 HlsGostCmac,
142 HlsGostStreebog,
144 HlsGostSignature,
146}
147
148impl AuthMechanism {
149 pub fn id(&self) -> u8 {
151 match self {
152 AuthMechanism::None => 0,
153 AuthMechanism::Lls => 1,
154 AuthMechanism::HlsManufacturer => 2,
155 AuthMechanism::HlsMd5 => 3,
156 AuthMechanism::HlsSha1 => 4,
157 AuthMechanism::HlsGmac => 5,
158 AuthMechanism::HlsSha256 => 6,
159 AuthMechanism::HlsEcdsa => 7,
160 AuthMechanism::HlsGostCmac => 8,
161 AuthMechanism::HlsGostStreebog => 9,
162 AuthMechanism::HlsGostSignature => 10,
163 }
164 }
165
166 pub fn from_id(id: u8) -> Option<AuthMechanism> {
168 Some(match id {
169 0 => AuthMechanism::None,
170 1 => AuthMechanism::Lls,
171 2 => AuthMechanism::HlsManufacturer,
172 3 => AuthMechanism::HlsMd5,
173 4 => AuthMechanism::HlsSha1,
174 5 => AuthMechanism::HlsGmac,
175 6 => AuthMechanism::HlsSha256,
176 7 => AuthMechanism::HlsEcdsa,
177 8 => AuthMechanism::HlsGostCmac,
178 9 => AuthMechanism::HlsGostStreebog,
179 10 => AuthMechanism::HlsGostSignature,
180 _ => return None,
181 })
182 }
183
184 pub fn oid(&self) -> [u8; 7] {
187 [0x60, 0x85, 0x74, 0x05, 0x08, 0x02, self.id()]
188 }
189
190 pub fn is_hls(&self) -> bool {
193 self.id() >= 2
194 }
195}
196
197#[cfg(test)]
198mod tests {
199 use super::*;
200
201 #[test]
202 fn security_suite_ids_and_key_lengths() {
203 assert_eq!(SecuritySuite::Suite0.id(), 0);
204 assert_eq!(SecuritySuite::Suite2.id(), 2);
205 assert_eq!(SecuritySuite::from_id(1), Some(SecuritySuite::Suite1));
206 assert_eq!(SecuritySuite::from_id(3), None);
207 assert_eq!(SecuritySuite::Suite1.aes_key_len(), 16);
208 assert_eq!(SecuritySuite::Suite2.aes_key_len(), 32);
209 assert!(!SecuritySuite::Suite0.has_public_key());
210 assert!(SecuritySuite::Suite2.has_public_key());
211 }
212
213 #[test]
214 fn security_policy_maps_to_control_byte() {
215 assert_eq!(SecurityPolicy::None.security_control_bits(), 0x00);
216 assert_eq!(SecurityPolicy::Authentication.security_control_bits(), 0x10);
217 assert_eq!(SecurityPolicy::Encryption.security_control_bits(), 0x20);
218 assert_eq!(SecurityPolicy::AuthenticationEncryption.security_control_bits(), 0x30);
219 assert_eq!(SecurityPolicy::AuthenticationEncryption.security_control_byte(SecuritySuite::Suite2), 0x32);
221 assert_eq!(SecurityPolicy::from_security_control(0x30), SecurityPolicy::AuthenticationEncryption);
222 assert_eq!(SecurityPolicy::from_security_control(0x10), SecurityPolicy::Authentication);
223 }
224
225 #[test]
226 fn all_mechanisms_round_trip_and_have_oids() {
227 for id in 0..=10u8 {
228 let m = AuthMechanism::from_id(id).unwrap();
229 assert_eq!(m.id(), id);
230 let oid = m.oid();
231 assert_eq!(&oid[..6], &[0x60, 0x85, 0x74, 0x05, 0x08, 0x02]);
232 assert_eq!(oid[6], id);
233 }
234 assert_eq!(AuthMechanism::from_id(11), None);
235 assert!(AuthMechanism::HlsGostStreebog.is_hls());
237 assert!(!AuthMechanism::Lls.is_hls());
238 }
239}