1#![cfg_attr(not(test), no_std)]
22#![deny(unsafe_code)]
23#![deny(clippy::all)]
24#![deny(clippy::pedantic)]
25#![recursion_limit = "256"]
26
27pub mod basic;
28pub mod constant;
29pub mod data;
30#[macro_use]
31pub mod r#macro;
32pub mod frame;
33
34#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
36#[repr(transparent)]
37pub struct TpmHandle(pub u32);
38
39impl core::convert::From<u32> for TpmHandle {
40 fn from(val: u32) -> Self {
41 Self(val)
42 }
43}
44
45impl core::convert::From<TpmHandle> for u32 {
46 fn from(val: TpmHandle) -> Self {
47 val.0
48 }
49}
50
51impl TpmMarshal for TpmHandle {
52 fn marshal(&self, writer: &mut TpmWriter) -> TpmResult<()> {
53 TpmMarshal::marshal(&self.0, writer)
54 }
55}
56
57impl TpmUnmarshal for TpmHandle {
58 fn unmarshal(buf: &[u8]) -> TpmResult<(Self, &[u8])> {
59 let (val, buf) = u32::unmarshal(buf)?;
60 Ok((Self(val), buf))
61 }
62}
63
64impl TpmSized for TpmHandle {
65 const SIZE: usize = size_of::<u32>();
66 fn len(&self) -> usize {
67 Self::SIZE
68 }
69}
70
71impl core::fmt::Display for TpmHandle {
72 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
73 core::fmt::Display::fmt(&self.0, f)
74 }
75}
76
77impl core::fmt::LowerHex for TpmHandle {
78 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
79 core::fmt::LowerHex::fmt(&self.0, f)
80 }
81}
82
83impl core::fmt::UpperHex for TpmHandle {
84 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
85 core::fmt::UpperHex::fmt(&self.0, f)
86 }
87}
88
89#[derive(Debug, PartialEq, Eq)]
90pub enum TpmDiscriminant {
91 Signed(i64),
92 Unsigned(u64),
93}
94
95impl core::fmt::LowerHex for TpmDiscriminant {
96 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
97 match self {
98 TpmDiscriminant::Signed(v) => write!(f, "{v:x}"),
99 TpmDiscriminant::Unsigned(v) => write!(f, "{v:x}"),
100 }
101 }
102}
103
104#[derive(Debug, PartialEq, Eq)]
105pub enum TpmProtocolError {
107 CapacityExceeded,
109 BufferExceeded,
111 ListExceeded,
113 InvalidDiscriminant(&'static str, TpmDiscriminant),
115 MalformedValue,
117 TrailingData,
119 UnexpectedEof,
121}
122
123impl core::fmt::Display for TpmProtocolError {
124 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
125 match self {
126 Self::CapacityExceeded => write!(f, "capacity exceeded"),
127 Self::BufferExceeded => write!(f, "buffer exceeded"),
128 Self::ListExceeded => write!(f, "list exceeded"),
129 Self::InvalidDiscriminant(type_name, value) => {
130 write!(f, "invalid discriminant: {type_name}: 0x{value:x}")
131 }
132 Self::MalformedValue => write!(f, "malformed value"),
133 Self::TrailingData => write!(f, "trailing data"),
134 Self::UnexpectedEof => write!(f, "unexpected EOF"),
135 }
136 }
137}
138
139impl core::error::Error for TpmProtocolError {}
140
141pub type TpmResult<T> = Result<T, TpmProtocolError>;
142
143pub struct TpmWriter<'a> {
145 buffer: &'a mut [u8],
146 cursor: usize,
147}
148
149impl<'a> TpmWriter<'a> {
150 #[must_use]
152 pub fn new(buffer: &'a mut [u8]) -> Self {
153 Self { buffer, cursor: 0 }
154 }
155
156 #[must_use]
158 pub fn len(&self) -> usize {
159 self.cursor
160 }
161
162 #[must_use]
164 pub fn is_empty(&self) -> bool {
165 self.cursor == 0
166 }
167
168 pub fn write_bytes(&mut self, bytes: &[u8]) -> TpmResult<()> {
175 let end = self.cursor + bytes.len();
176 if end > self.buffer.len() {
177 return Err(TpmProtocolError::BufferExceeded);
178 }
179 self.buffer[self.cursor..end].copy_from_slice(bytes);
180 self.cursor = end;
181 Ok(())
182 }
183}
184
185pub trait TpmSized {
188 const SIZE: usize;
191
192 fn len(&self) -> usize;
194
195 fn is_empty(&self) -> bool {
197 self.len() == 0
198 }
199}
200
201pub trait TpmMarshal: TpmSized {
202 fn marshal(&self, writer: &mut TpmWriter) -> TpmResult<()>;
208}
209
210pub trait TpmUnmarshal: Sized + TpmSized {
211 fn unmarshal(buf: &[u8]) -> TpmResult<(Self, &[u8])>;
219}
220
221pub trait TpmTagged {
223 type Tag: TpmUnmarshal + TpmMarshal + Copy;
225 type Value;
227}
228
229pub trait TpmUnmarshalTagged: Sized {
231 fn unmarshal_tagged(tag: <Self as TpmTagged>::Tag, buf: &[u8]) -> TpmResult<(Self, &[u8])>
239 where
240 Self: TpmTagged,
241 <Self as TpmTagged>::Tag: TpmUnmarshal + TpmMarshal;
242}
243
244tpm_integer!(u8, Unsigned);
245tpm_integer!(i8, Signed);
246tpm_integer!(i32, Signed);
247tpm_integer!(u16, Unsigned);
248tpm_integer!(u32, Unsigned);
249tpm_integer!(u64, Unsigned);