Skip to main content

tpm2_protocol/frame/
mod.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// Copyright (c) 2025 Opinsys Oy
3// Copyright (c) 2024-2025 Jarkko Sakkinen
4
5use crate::{TpmMarshal, TpmResult, TpmSized, TpmWriter, basic::TpmList};
6use core::fmt::Debug;
7
8mod data;
9mod marshal;
10mod unmarshal;
11mod wire;
12
13pub use self::{data::*, marshal::*, unmarshal::*, wire::*};
14
15use crate::constant::{MAX_HANDLES, MAX_SESSIONS};
16
17const TPM_HEADER_SIZE: u32 = 10;
18
19/// A fixed-capacity list for TPM handles.
20pub type TpmHandles = TpmList<crate::basic::TpmHandle, MAX_HANDLES>;
21
22/// A fixed-capacity list for command authorization sessions.
23pub type TpmAuthCommands = TpmList<crate::data::TpmsAuthCommand, MAX_SESSIONS>;
24
25/// A fixed-capacity list for response authorization sessions.
26pub type TpmAuthResponses = TpmList<crate::data::TpmsAuthResponse, MAX_SESSIONS>;
27
28/// A trait for TPM commands and responses that provides static header information.
29pub trait TpmHeader {
30    /// The Command Code (CC) for the command or response.
31    const CC: crate::data::TpmCc;
32    /// The number of handles in the handle area.
33    const HANDLES: usize;
34}
35
36/// A trait for TPM commands and responses that provides dynamic frame information.
37pub trait TpmFrame: TpmMarshal + TpmMarshalBody + Debug {
38    /// Returns the Command Code (CC) for the command or response.
39    fn cc(&self) -> crate::data::TpmCc;
40    /// Returns the number of handles in the handle area.
41    fn handles(&self) -> usize;
42}
43
44/// A trait for marshaling command/response bodies in separate handle and parameter sections.
45pub trait TpmMarshalBody: TpmSized {
46    /// Marshals the handle area.
47    ///
48    /// # Errors
49    ///
50    /// Returns `Err(TpmProtocolError)` on a marshal failure.
51    fn marshal_handles(&self, writer: &mut TpmWriter) -> TpmResult<()>;
52
53    /// Marshals the parameter area.
54    ///
55    /// # Errors
56    ///
57    /// Returns `Err(TpmProtocolError)` on a marshal failure.
58    fn marshal_parameters(&self, writer: &mut TpmWriter) -> TpmResult<()>;
59}
60
61/// Unmarshals a command body from the slices point out to the handle area and
62/// parameter area of the original buffer.
63pub(crate) trait TpmUnmarshalCommand: Sized {
64    /// Unmarshals the command body from the handle and parameter area.
65    ///
66    /// # Errors
67    ///
68    /// Returns `Err(TpmProtocolError)` on a unmarshal failure.
69    fn unmarshal_body<'a>(handles: &'a [u8], params: &'a [u8]) -> TpmResult<(Self, &'a [u8])>;
70}
71
72/// Unmarshals a response body using the response tag to handle structural variations.
73pub trait TpmUnmarshalResponse: Sized {
74    /// Unmarshals the response body from a buffer, using the response tag
75    /// dynamically to determine the structure.
76    ///
77    /// # Errors
78    ///
79    /// Returns `Err(TpmProtocolError)` on a unmarshal failure.
80    fn unmarshal_body(tag: crate::data::TpmSt, buf: &[u8]) -> TpmResult<(Self, &[u8])>;
81}
82
83tpm_dispatch! {
84    (TpmNvUndefineSpaceSpecialCommand, TpmNvUndefineSpaceSpecialResponse, NvUndefineSpaceSpecial),
85    (TpmEvictControlCommand, TpmEvictControlResponse, EvictControl),
86    (TpmHierarchyControlCommand, TpmHierarchyControlResponse, HierarchyControl),
87    (TpmNvUndefineSpaceCommand, TpmNvUndefineSpaceResponse, NvUndefineSpace),
88    (TpmChangeEpsCommand, TpmChangeEpsResponse, ChangeEps),
89    (TpmChangePpsCommand, TpmChangePpsResponse, ChangePps),
90    (TpmClearCommand, TpmClearResponse, Clear),
91    (TpmClearControlCommand, TpmClearControlResponse, ClearControl),
92    (TpmClockSetCommand, TpmClockSetResponse, ClockSet),
93    (TpmHierarchyChangeAuthCommand, TpmHierarchyChangeAuthResponse, HierarchyChangeAuth),
94    (TpmNvDefineSpaceCommand, TpmNvDefineSpaceResponse, NvDefineSpace),
95    (TpmPcrAllocateCommand, TpmPcrAllocateResponse, PcrAllocate),
96    (TpmPcrSetAuthPolicyCommand, TpmPcrSetAuthPolicyResponse, PcrSetAuthPolicy),
97    (TpmPpCommandsCommand, TpmPpCommandsResponse, PpCommands),
98    (TpmSetPrimaryPolicyCommand, TpmSetPrimaryPolicyResponse, SetPrimaryPolicy),
99    (TpmFieldUpgradeStartCommand, TpmFieldUpgradeStartResponse, FieldUpgradeStart),
100    (TpmClockRateAdjustCommand, TpmClockRateAdjustResponse, ClockRateAdjust),
101    (TpmCreatePrimaryCommand, TpmCreatePrimaryResponse, CreatePrimary),
102    (TpmNvGlobalWriteLockCommand, TpmNvGlobalWriteLockResponse, NvGlobalWriteLock),
103    (TpmGetCommandAuditDigestCommand, TpmGetCommandAuditDigestResponse, GetCommandAuditDigest),
104    (TpmNvIncrementCommand, TpmNvIncrementResponse, NvIncrement),
105    (TpmNvSetBitsCommand, TpmNvSetBitsResponse, NvSetBits),
106    (TpmNvExtendCommand, TpmNvExtendResponse, NvExtend),
107    (TpmNvWriteCommand, TpmNvWriteResponse, NvWrite),
108    (TpmNvWriteLockCommand, TpmNvWriteLockResponse, NvWriteLock),
109    (TpmDictionaryAttackLockResetCommand, TpmDictionaryAttackLockResetResponse, DictionaryAttackLockReset),
110    (TpmDictionaryAttackParametersCommand, TpmDictionaryAttackParametersResponse, DictionaryAttackParameters),
111    (TpmNvChangeAuthCommand, TpmNvChangeAuthResponse, NvChangeAuth),
112    (TpmPcrEventCommand, TpmPcrEventResponse, PcrEvent),
113    (TpmPcrResetCommand, TpmPcrResetResponse, PcrReset),
114    (TpmSequenceCompleteCommand, TpmSequenceCompleteResponse, SequenceComplete),
115    (TpmSetAlgorithmSetCommand, TpmSetAlgorithmSetResponse, SetAlgorithmSet),
116    (TpmSetCommandCodeAuditStatusCommand, TpmSetCommandCodeAuditStatusResponse, SetCommandCodeAuditStatus),
117    (TpmFieldUpgradeDataCommand, TpmFieldUpgradeDataResponse, FieldUpgradeData),
118    (TpmIncrementalSelfTestCommand, TpmIncrementalSelfTestResponse, IncrementalSelfTest),
119    (TpmSelfTestCommand, TpmSelfTestResponse, SelfTest),
120    (TpmStartupCommand, TpmStartupResponse, Startup),
121    (TpmShutdownCommand, TpmShutdownResponse, Shutdown),
122    (TpmStirRandomCommand, TpmStirRandomResponse, StirRandom),
123    (TpmActivateCredentialCommand, TpmActivateCredentialResponse, ActivateCredential),
124    (TpmCertifyCommand, TpmCertifyResponse, Certify),
125    (TpmPolicyNvCommand, TpmPolicyNvResponse, PolicyNv),
126    (TpmCertifyCreationCommand, TpmCertifyCreationResponse, CertifyCreation),
127    (TpmDuplicateCommand, TpmDuplicateResponse, Duplicate),
128    (TpmGetTimeCommand, TpmGetTimeResponse, GetTime),
129    (TpmGetSessionAuditDigestCommand, TpmGetSessionAuditDigestResponse, GetSessionAuditDigest),
130    (TpmNvReadCommand, TpmNvReadResponse, NvRead),
131    (TpmNvReadLockCommand, TpmNvReadLockResponse, NvReadLock),
132    (TpmObjectChangeAuthCommand, TpmObjectChangeAuthResponse, ObjectChangeAuth),
133    (TpmPolicySecretCommand, TpmPolicySecretResponse, PolicySecret),
134    (TpmRewrapCommand, TpmRewrapResponse, Rewrap),
135    (TpmCreateCommand, TpmCreateResponse, Create),
136    (TpmEcdhZGenCommand, TpmEcdhZGenResponse, EcdhZGen),
137    (TpmHmacCommand, TpmHmacResponse, Hmac),
138    (TpmImportCommand, TpmImportResponse, Import),
139    (TpmLoadCommand, TpmLoadResponse, Load),
140    (TpmQuoteCommand, TpmQuoteResponse, Quote),
141    (TpmRsaDecryptCommand, TpmRsaDecryptResponse, RsaDecrypt),
142    (TpmHmacStartCommand, TpmHmacStartResponse, HmacStart),
143    (TpmSequenceUpdateCommand, TpmSequenceUpdateResponse, SequenceUpdate),
144    (TpmSignCommand, TpmSignResponse, Sign),
145    (TpmUnsealCommand, TpmUnsealResponse, Unseal),
146    (TpmPolicySignedCommand, TpmPolicySignedResponse, PolicySigned),
147    (TpmContextLoadCommand, TpmContextLoadResponse, ContextLoad),
148    (TpmContextSaveCommand, TpmContextSaveResponse, ContextSave),
149    (TpmEcdhKeyGenCommand, TpmEcdhKeyGenResponse, EcdhKeyGen),
150    (TpmEncryptDecryptCommand, TpmEncryptDecryptResponse, EncryptDecrypt),
151    (TpmFlushContextCommand, TpmFlushContextResponse, FlushContext),
152    (TpmLoadExternalCommand, TpmLoadExternalResponse, LoadExternal),
153    (TpmMakeCredentialCommand, TpmMakeCredentialResponse, MakeCredential),
154    (TpmNvReadPublicCommand, TpmNvReadPublicResponse, NvReadPublic),
155    (TpmPolicyAuthorizeCommand, TpmPolicyAuthorizeResponse, PolicyAuthorize),
156    (TpmPolicyAuthValueCommand, TpmPolicyAuthValueResponse, PolicyAuthValue),
157    (TpmPolicyCommandCodeCommand, TpmPolicyCommandCodeResponse, PolicyCommandCode),
158    (TpmPolicyCounterTimerCommand, TpmPolicyCounterTimerResponse, PolicyCounterTimer),
159    (TpmPolicyCpHashCommand, TpmPolicyCpHashResponse, PolicyCpHash),
160    (TpmPolicyLocalityCommand, TpmPolicyLocalityResponse, PolicyLocality),
161    (TpmPolicyNameHashCommand, TpmPolicyNameHashResponse, PolicyNameHash),
162    (TpmPolicyOrCommand, TpmPolicyOrResponse, PolicyOr),
163    (TpmPolicyTicketCommand, TpmPolicyTicketResponse, PolicyTicket),
164    (TpmReadPublicCommand, TpmReadPublicResponse, ReadPublic),
165    (TpmRsaEncryptCommand, TpmRsaEncryptResponse, RsaEncrypt),
166    (TpmStartAuthSessionCommand, TpmStartAuthSessionResponse, StartAuthSession),
167    (TpmVerifySignatureCommand, TpmVerifySignatureResponse, VerifySignature),
168    (TpmEccParametersCommand, TpmEccParametersResponse, EccParameters),
169    (TpmFirmwareReadCommand, TpmFirmwareReadResponse, FirmwareRead),
170    (TpmGetCapabilityCommand, TpmGetCapabilityResponse, GetCapability),
171    (TpmGetRandomCommand, TpmGetRandomResponse, GetRandom),
172    (TpmGetTestResultCommand, TpmGetTestResultResponse, GetTestResult),
173    (TpmHashCommand, TpmHashResponse, Hash),
174    (TpmPcrReadCommand, TpmPcrReadResponse, PcrRead),
175    (TpmPolicyPcrCommand, TpmPolicyPcrResponse, PolicyPcr),
176    (TpmPolicyRestartCommand, TpmPolicyRestartResponse, PolicyRestart),
177    (TpmReadClockCommand, TpmReadClockResponse, ReadClock),
178    (TpmPcrExtendCommand, TpmPcrExtendResponse, PcrExtend),
179    (TpmPcrSetAuthValueCommand, TpmPcrSetAuthValueResponse, PcrSetAuthValue),
180    (TpmNvCertifyCommand, TpmNvCertifyResponse, NvCertify),
181    (TpmEventSequenceCompleteCommand, TpmEventSequenceCompleteResponse, EventSequenceComplete),
182    (TpmHashSequenceStartCommand, TpmHashSequenceStartResponse, HashSequenceStart),
183    (TpmPolicyPhysicalPresenceCommand, TpmPolicyPhysicalPresenceResponse, PolicyPhysicalPresence),
184    (TpmPolicyDuplicationSelectCommand, TpmPolicyDuplicationSelectResponse, PolicyDuplicationSelect),
185    (TpmPolicyGetDigestCommand, TpmPolicyGetDigestResponse, PolicyGetDigest),
186    (TpmTestParmsCommand, TpmTestParmsResponse, TestParms),
187    (TpmCommitCommand, TpmCommitResponse, Commit),
188    (TpmPolicyPasswordCommand, TpmPolicyPasswordResponse, PolicyPassword),
189    (TpmZGen2PhaseCommand, TpmZGen2PhaseResponse, ZGen2Phase),
190    (TpmEcEphemeralCommand, TpmEcEphemeralResponse, EcEphemeral),
191    (TpmPolicyNvWrittenCommand, TpmPolicyNvWrittenResponse, PolicyNvWritten),
192    (TpmPolicyTemplateCommand, TpmPolicyTemplateResponse, PolicyTemplate),
193    (TpmCreateLoadedCommand, TpmCreateLoadedResponse, CreateLoaded),
194    (TpmPolicyAuthorizeNvCommand, TpmPolicyAuthorizeNvResponse, PolicyAuthorizeNv),
195    (TpmEncryptDecrypt2Command, TpmEncryptDecrypt2Response, EncryptDecrypt2),
196    (TpmAcGetCapabilityCommand, TpmAcGetCapabilityResponse, AcGetCapability),
197    (TpmAcSendCommand, TpmAcSendResponse, AcSend),
198    (TpmPolicyAcSendSelectCommand, TpmPolicyAcSendSelectResponse, PolicyAcSendSelect),
199    (TpmActSetTimeoutCommand, TpmActSetTimeoutResponse, ActSetTimeout),
200    (TpmEccEncryptCommand, TpmEccEncryptResponse, EccEncrypt),
201    (TpmEccDecryptCommand, TpmEccDecryptResponse, EccDecrypt),
202    (TpmPolicyCapabilityCommand, TpmPolicyCapabilityResponse, PolicyCapability),
203    (TpmPolicyParametersCommand, TpmPolicyParametersResponse, PolicyParameters),
204    (TpmNvDefineSpace2Command, TpmNvDefineSpace2Response, NvDefineSpace2),
205    (TpmNvReadPublic2Command, TpmNvReadPublic2Response, NvReadPublic2),
206    (TpmReadOnlyControlCommand, TpmReadOnlyControlResponse, ReadOnlyControl),
207    (TpmPolicyTransportSpdmCommand, TpmPolicyTransportSpdmResponse, PolicyTransportSpdm),
208    (TpmVendorTcgTestCommand, TpmVendorTcgTestResponse, VendorTcgTest),
209}