ecc608_linux/
key_config.rs1use bitfield::bitfield;
2use bytes::Buf;
3use serde_derive::Serialize;
4
5#[derive(Serialize, Debug, PartialEq, Eq)]
6#[serde(rename_all = "lowercase")]
7pub enum KeyConfigType {
8 Ecc,
9 NotEcc,
10}
11
12impl From<u8> for KeyConfigType {
13 fn from(v: u8) -> Self {
14 match v & 4 == 4 {
15 true => Self::Ecc,
16 _ => Self::NotEcc,
17 }
18 }
19}
20
21impl From<KeyConfigType> for u8 {
22 fn from(v: KeyConfigType) -> Self {
23 match v {
24 KeyConfigType::Ecc => 4,
25 KeyConfigType::NotEcc => 7,
26 }
27 }
28}
29
30impl From<&[u8]> for KeyConfig {
31 fn from(v: &[u8]) -> Self {
32 let mut buf = v;
33 Self(buf.get_u16())
34 }
35}
36
37bitfield! {
38 #[derive(PartialEq, Eq)]
39 pub struct KeyConfig(u16);
40 impl Debug;
41
42 pub u8, auth_key, set_auth_key: 3, 0;
43 pub intrusion_disable, set_intrusion_disable: 4;
44 pub u8, x509_index, set_x509_index: 7, 6;
45
46 pub private, set_private: 8;
47 pub pub_info, set_pub_info: 9;
48 pub u8, from into KeyConfigType, key_type, set_key_type: 12, 10;
49 pub lockable, set_is_lockable: 13;
50 pub req_random, set_req_random: 14;
51 pub req_auth, set_req_auth: 15;
52}
53
54impl From<u16> for KeyConfig {
55 fn from(v: u16) -> Self {
56 Self(v)
57 }
58}
59
60impl From<KeyConfig> for u16 {
61 fn from(v: KeyConfig) -> Self {
62 v.0
63 }
64}
65
66impl From<&KeyConfig> for u16 {
67 fn from(v: &KeyConfig) -> Self {
68 v.0
69 }
70}
71
72impl Default for KeyConfig {
74 fn default() -> Self {
75 let mut result = KeyConfig(0);
76 result.set_key_type(KeyConfigType::Ecc);
77 result.set_is_lockable(true);
78 result.set_private(true);
79 result.set_pub_info(true);
80 result
81 }
82}
83
84impl serde::ser::Serialize for KeyConfig {
85 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
86 where
87 S: serde::ser::Serializer,
88 {
89 use serde::ser::SerializeStruct;
90 let mut state = serializer.serialize_struct("key_config", 9)?;
91 state.serialize_field("auth_key", &self.auth_key())?;
92 state.serialize_field("intrusion_disable", &self.intrusion_disable())?;
93 state.serialize_field("x509_index", &self.x509_index())?;
94 state.serialize_field("private", &self.private())?;
95 state.serialize_field("pub_info", &self.pub_info())?;
96 state.serialize_field("key_type", &self.key_type())?;
97 state.serialize_field("lockable", &self.lockable())?;
98 state.serialize_field("req_random", &self.req_random())?;
99 state.serialize_field("req_auth", &self.req_auth())?;
100
101 state.end()
102 }
103}