1use crate::error::Error;
10use alloc::vec::Vec;
11
12#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct PivData {
18 pub object_id: [u8; 3],
20 pub element_id: u8,
22 pub offset: u16,
24 pub data: Vec<u8>,
26}
27
28impl PivData {
29 pub fn encode(&self) -> Result<Vec<u8>, Error> {
31 let mut out = Vec::with_capacity(6 + self.data.len());
32 out.extend_from_slice(&self.object_id);
33 out.push(self.element_id);
34 out.extend_from_slice(&self.offset.to_le_bytes());
35 out.extend_from_slice(&self.data);
36 Ok(out)
37 }
38
39 pub fn decode(data: &[u8]) -> Result<Self, Error> {
41 if data.len() < 6 {
42 return Err(Error::MalformedPayload {
43 code: 0xA3,
44 reason: "PIVDATA requires at least 6 bytes",
45 });
46 }
47 let mut object_id = [0u8; 3];
48 object_id.copy_from_slice(&data[..3]);
49 Ok(Self {
50 object_id,
51 element_id: data[3],
52 offset: u16::from_le_bytes([data[4], data[5]]),
53 data: data[6..].to_vec(),
54 })
55 }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq)]
60pub struct GenAuth {
61 pub algorithm: u8,
63 pub key_ref: u8,
65 pub auth_template: Vec<u8>,
67}
68
69impl GenAuth {
70 pub fn encode(&self) -> Result<Vec<u8>, Error> {
72 let mut out = Vec::with_capacity(2 + self.auth_template.len());
73 out.push(self.algorithm);
74 out.push(self.key_ref);
75 out.extend_from_slice(&self.auth_template);
76 Ok(out)
77 }
78
79 pub fn decode(data: &[u8]) -> Result<Self, Error> {
81 if data.len() < 2 {
82 return Err(Error::MalformedPayload {
83 code: 0xA4,
84 reason: "GENAUTH requires at least 2 bytes",
85 });
86 }
87 Ok(Self {
88 algorithm: data[0],
89 key_ref: data[1],
90 auth_template: data[2..].to_vec(),
91 })
92 }
93}
94
95#[derive(Debug, Clone, PartialEq, Eq)]
97pub struct CrAuth {
98 pub challenge: Vec<u8>,
100}
101
102impl CrAuth {
103 pub fn encode(&self) -> Result<Vec<u8>, Error> {
105 Ok(self.challenge.clone())
106 }
107
108 pub fn decode(data: &[u8]) -> Result<Self, Error> {
110 Ok(Self {
111 challenge: data.to_vec(),
112 })
113 }
114}