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