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