tpm2_protocol/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// Copyright (c) 2025 Opinsys Oy
3// Copyright (c) 2024-2025 Jarkko Sakkinen
4
5//! # TPM 2.0 Protocol
6//!
7//! A library for marshaling and unmarshaling TCG TPM 2.0 protocol messages.
8//!
9//! ## Constraints
10//!
11//! * `alloc` is disallowed.
12//! * Dependencies are disallowed.
13//! * Developer dependencies are disallowed.
14//! * Panics are disallowed.
15//!
16//! ## Design Goals
17//!
18//! * The crate must compile with GNU make and rustc without any external
19//!   dependencies.
20
21#![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/// A TPM handle, which is a 32-bit unsigned integer.
35#[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)]
105/// TPM protocol error.
106pub enum TpmError {
107    /// Capacity of a structure has been exceeded,
108    CapacityExceeded,
109    /// Unknown discriminant.
110    InvalidDiscriminant(&'static str, TpmDiscriminant),
111    /// The frame or object is malformed.
112    Malformed,
113    /// Trailing data left.
114    Trailing,
115    /// Not enough bytes to unmarshal.
116    Truncated,
117}
118
119impl core::fmt::Display for TpmError {
120    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
121        match self {
122            Self::CapacityExceeded => write!(f, "capacity has been exceeded"),
123            Self::InvalidDiscriminant(type_name, value) => {
124                write!(f, "unknown discriminant: {type_name}:  0x{value:x}")
125            }
126            Self::Malformed => write!(f, "data is malformed"),
127            Self::Trailing => write!(f, "trailing data left"),
128            Self::Truncated => write!(f, "data is truncated"),
129        }
130    }
131}
132
133pub type TpmResult<T> = Result<T, TpmError>;
134
135/// Writes into a mutable byte slice.
136pub struct TpmWriter<'a> {
137    buffer: &'a mut [u8],
138    cursor: usize,
139}
140
141impl<'a> TpmWriter<'a> {
142    /// Creates a new writer for the given buffer.
143    #[must_use]
144    pub fn new(buffer: &'a mut [u8]) -> Self {
145        Self { buffer, cursor: 0 }
146    }
147
148    /// Returns the number of bytes written so far.
149    #[must_use]
150    pub fn len(&self) -> usize {
151        self.cursor
152    }
153
154    /// Returns `true` if no bytes have been written.
155    #[must_use]
156    pub fn is_empty(&self) -> bool {
157        self.cursor == 0
158    }
159
160    /// Appends a slice of bytes to the writer.
161    ///
162    /// # Errors
163    ///
164    /// Returns `TpmError::CapacityExceeded` if the writer does not have enough
165    /// capacity to hold the new bytes.
166    pub fn write_bytes(&mut self, bytes: &[u8]) -> TpmResult<()> {
167        let end = self.cursor + bytes.len();
168        if end > self.buffer.len() {
169            return Err(TpmError::CapacityExceeded);
170        }
171        self.buffer[self.cursor..end].copy_from_slice(bytes);
172        self.cursor = end;
173        Ok(())
174    }
175}
176
177/// Provides two ways to determine the size of an object: a compile-time maximum
178/// and a runtime exact size.
179pub trait TpmSized {
180    /// The estimated size of the object in its serialized form evaluated at
181    /// compile-time (always larger than the realized length).
182    const SIZE: usize;
183
184    /// Returns the exact serialized size of the object.
185    fn len(&self) -> usize;
186
187    /// Returns `true` if the object has a serialized length of zero.
188    fn is_empty(&self) -> bool {
189        self.len() == 0
190    }
191}
192
193pub trait TpmMarshal: TpmSized {
194    /// Marshals the object into the given writer.
195    ///
196    /// # Errors
197    ///
198    /// Returns `Err(TpmError)` on a marshal failure.
199    fn marshal(&self, writer: &mut TpmWriter) -> TpmResult<()>;
200}
201
202pub trait TpmUnmarshal: Sized + TpmSized {
203    /// Unmarshals an object from the given buffer.
204    ///
205    /// Returns the unmarshald type and the remaining portion of the buffer.
206    ///
207    /// # Errors
208    ///
209    /// Returns `Err(TpmError)` on a unmarshal failure.
210    fn unmarshal(buf: &[u8]) -> TpmResult<(Self, &[u8])>;
211}
212
213/// Types that are composed of a tag and a value e.g., a union.
214pub trait TpmTagged {
215    /// The type of the tag/discriminant.
216    type Tag: TpmUnmarshal + TpmMarshal + Copy;
217    /// The type of the value/union.
218    type Value;
219}
220
221/// Unmarshals a tagged object from a buffer.
222pub trait TpmUnmarshalTagged: Sized {
223    /// Unmarshals a tagged object from the given buffer using the provided tag.
224    ///
225    /// # Errors
226    ///
227    /// This method can return any error of the underlying type's `TpmUnmarshal` implementation,
228    /// such as a `TpmError::Truncated` if the buffer is too small or an
229    /// `TpmError::Malformed` if the data is malformed.
230    fn unmarshal_tagged(tag: <Self as TpmTagged>::Tag, buf: &[u8]) -> TpmResult<(Self, &[u8])>
231    where
232        Self: TpmTagged,
233        <Self as TpmTagged>::Tag: TpmUnmarshal + TpmMarshal;
234}
235
236tpm_integer!(u8, Unsigned);
237tpm_integer!(i8, Signed);
238tpm_integer!(i32, Signed);
239tpm_integer!(u16, Unsigned);
240tpm_integer!(u32, Unsigned);
241tpm_integer!(u64, Unsigned);