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)]
91pub enum TpmProtocolError {
92 CapacityExceeded,
95 InvalidAttestMagic,
98 InvalidTag,
101 InvalidValue,
103 InvalidVariant,
105 OperationFailed,
107 TooManyItems,
110 TrailingData,
112 UnexpectedEnd,
114}
115
116impl core::fmt::Display for TpmProtocolError {
117 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
118 match self {
119 Self::CapacityExceeded => write!(f, "buffer capacity exceeded"),
120 Self::InvalidAttestMagic => write!(f, "invalid attestation magiC"),
121 Self::InvalidTag => write!(f, "invalid tag"),
122 Self::InvalidValue => write!(f, "invalid value"),
123 Self::InvalidVariant => write!(f, "invalid variant"),
124 Self::OperationFailed => write!(f, "operation failed"),
125 Self::TooManyItems => write!(f, "too many list items"),
126 Self::TrailingData => write!(f, "trailing data"),
127 Self::UnexpectedEnd => write!(f, "unexpected end"),
128 }
129 }
130}
131
132impl core::error::Error for TpmProtocolError {}
133
134pub type TpmResult<T> = Result<T, TpmProtocolError>;
135
136pub struct TpmWriter<'a> {
138 buffer: &'a mut [u8],
139 cursor: usize,
140}
141
142impl<'a> TpmWriter<'a> {
143 #[must_use]
145 pub fn new(buffer: &'a mut [u8]) -> Self {
146 Self { buffer, cursor: 0 }
147 }
148
149 #[must_use]
151 pub fn len(&self) -> usize {
152 self.cursor
153 }
154
155 #[must_use]
157 pub fn is_empty(&self) -> bool {
158 self.cursor == 0
159 }
160
161 pub fn write_bytes(&mut self, bytes: &[u8]) -> TpmResult<()> {
168 let end = self.cursor + bytes.len();
169 if end > self.buffer.len() {
170 return Err(TpmProtocolError::CapacityExceeded);
171 }
172 self.buffer[self.cursor..end].copy_from_slice(bytes);
173 self.cursor = end;
174 Ok(())
175 }
176}
177
178pub trait TpmSized {
181 const SIZE: usize;
184
185 fn len(&self) -> usize;
187
188 fn is_empty(&self) -> bool {
190 self.len() == 0
191 }
192}
193
194pub trait TpmMarshal: TpmSized {
195 fn marshal(&self, writer: &mut TpmWriter) -> TpmResult<()>;
201}
202
203pub trait TpmUnmarshal: Sized + TpmSized {
204 fn unmarshal(buf: &[u8]) -> TpmResult<(Self, &[u8])>;
212}
213
214pub trait TpmTagged {
216 type Tag: TpmUnmarshal + TpmMarshal + Copy;
218 type Value;
220}
221
222pub trait TpmUnmarshalTagged: Sized {
224 fn unmarshal_tagged(tag: <Self as TpmTagged>::Tag, buf: &[u8]) -> TpmResult<(Self, &[u8])>
232 where
233 Self: TpmTagged,
234 <Self as TpmTagged>::Tag: TpmUnmarshal + TpmMarshal;
235}
236
237tpm_integer!(u8, Unsigned);
238tpm_integer!(i8, Signed);
239tpm_integer!(i32, Signed);
240tpm_integer!(u16, Unsigned);
241tpm_integer!(u32, Unsigned);
242tpm_integer!(u64, Unsigned);