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