1use 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
19pub type TpmHandles = TpmList<crate::basic::TpmHandle, MAX_HANDLES>;
21
22pub type TpmAuthCommands = TpmList<crate::data::TpmsAuthCommand, MAX_SESSIONS>;
24
25pub type TpmAuthResponses = TpmList<crate::data::TpmsAuthResponse, MAX_SESSIONS>;
27
28pub trait TpmHeader {
30 const CC: crate::data::TpmCc;
32 const HANDLES: usize;
34}
35
36pub trait TpmFrame: TpmMarshal + TpmMarshalBody + Debug {
38 fn cc(&self) -> crate::data::TpmCc;
40 fn handles(&self) -> usize;
42}
43
44pub trait TpmMarshalBody: TpmSized {
46 fn marshal_handles(&self, writer: &mut TpmWriter) -> TpmResult<()>;
52
53 fn marshal_parameters(&self, writer: &mut TpmWriter) -> TpmResult<()>;
59}
60
61pub(crate) trait TpmUnmarshalCommand: Sized {
64 fn unmarshal_body<'a>(handles: &'a [u8], params: &'a [u8]) -> TpmResult<(Self, &'a [u8])>;
70}
71
72pub trait TpmUnmarshalResponse: Sized {
74 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}