blob: 593686b193ddfddcee51d6aabd8d940eacd37bb5 [file] [log] [blame]
Vadim Bendebury56797522015-05-20 10:32:25 -07001// This file was extracted from the TCG Published
2// Trusted Platform Module Library
3// Part 4: Supporting Routines
4// Family "2.0"
5// Level 00 Revision 01.16
6// October 30, 2014
7
8#ifndef GLOBAL_H
9#define GLOBAL_H
10//#define SELF_TEST
11#include "TpmBuildSwitches.h"
12#include "Tpm.h"
13#include "TPMB.h"
14#include "CryptoEngine.h"
Stefan Reinauer0994ac92016-01-28 15:28:59 -080015#ifndef EMBEDDED_MODE
Vadim Bendebury56797522015-05-20 10:32:25 -070016#include <setjmp.h>
Stefan Reinauer0994ac92016-01-28 15:28:59 -080017#endif
Vadim Bendebury56797522015-05-20 10:32:25 -070018//
19//
20//
21// Defines and Types
22//
23// Unreferenced Parameter
24//
25// This define is used to eliminate the compiler warning about an unreferenced parameter. Basically, it tells
26// the compiler that it is not an accident that the parameter is unreferenced.
27//
28#ifndef UNREFERENCED_PARAMETER
29# define UNREFERENCED_PARAMETER(a) (a)
30#endif
31#include "bits.h"
32//
33//
34// Crypto Self-Test Values
35//
36// Define these values here if the AlgorithmTests() project is not used
37//
38#ifndef SELF_TEST
39extern ALGORITHM_VECTOR g_implementedAlgorithms;
40extern ALGORITHM_VECTOR g_toTest;
41#else
42LIB_IMPORT extern ALGORITHM_VECTOR g_implementedAlgorithms;
43LIB_IMPORT extern ALGORITHM_VECTOR g_toTest;
44#endif
45//
46// These macros are used in CryptUtil() to invoke the incremental self test.
47//
48#define TEST(alg) if(TEST_BIT(alg, g_toTest)) CryptTestAlgorithm(alg, NULL)
49//
50// Use of TPM_ALG_NULL is reserved for RSAEP/RSADP testing. If someone is wanting to test a hash with
51// that value, don't do it.
52//
53#define TEST_HASH(alg) \
54 if( TEST_BIT(alg, g_toTest) \
55 && (alg != ALG_NULL_VALUE)) \
56 CryptTestAlgorithm(alg, NULL)
57//
58//
59// Hash and HMAC State Structures
60//
61// These definitions are for the types that can be in a hash state structure. These types are used in the
62// crypto utilities
63//
64typedef BYTE HASH_STATE_TYPE;
65#define HASH_STATE_EMPTY ((HASH_STATE_TYPE) 0)
66#define HASH_STATE_HASH ((HASH_STATE_TYPE) 1)
67#define HASH_STATE_HMAC ((HASH_STATE_TYPE) 2)
68//
69// A HASH_STATE structure contains an opaque hash stack state. A caller would use this structure when
70// performing incremental hash operations. The state is updated on each call. If type is an HMAC_STATE,
71// or HMAC_STATE_SEQUENCE then state is followed by the HMAC key in oPad format.
72//
73typedef struct
74{
75 CPRI_HASH_STATE state; // hash state
76 HASH_STATE_TYPE type; // type of the context
77} HASH_STATE;
78//
79//
80//
81//
82// An HMAC_STATE structure contains an opaque HMAC stack state. A caller would use this structure
83// when performing incremental HMAC operations. This structure contains a hash state and an HMAC key
84// and allows slightly better stack optimization than adding an HMAC key to each hash state.
85//
86typedef struct
87{
88 HASH_STATE hashState; // the hash state
89 TPM2B_HASH_BLOCK hmacKey; // the HMAC key
90} HMAC_STATE;
91//
92//
93// Other Types
94//
95// An AUTH_VALUE is a BYTE array containing a digest (TPMU_HA)
96//
97typedef BYTE AUTH_VALUE[sizeof(TPMU_HA)];
98//
99// A TIME_INFO is a BYTE array that can contain a TPMS_TIME_INFO
100//
101typedef BYTE TIME_INFO[sizeof(TPMS_TIME_INFO)];
102//
103// A NAME is a BYTE array that can contain a TPMU_NAME
104//
105typedef BYTE NAME[sizeof(TPMU_NAME)];
106//
107//
108// Loaded Object Structures
109//
110// Description
111//
112// The structures in this section define the object layout as it exists in TPM memory.
113// Two types of objects are defined: an ordinary object such as a key, and a sequence object that may be a
114// hash, HMAC, or event.
115//
116// OBJECT_ATTRIBUTES
117//
118// An OBJECT_ATTRIBUTES structure contains the variable attributes of an object. These properties are
119// not part of the public properties but are used by the TPM in managing the object. An
120// OBJECT_ATTRIBUTES is used in the definition of the OBJECT data type.
121//
122typedef struct
123{
124 unsigned publicOnly : 1; //0) SET if only the public portion of
125 // an object is loaded
126 unsigned epsHierarchy : 1; //1) SET if the object belongs to EPS
127 // Hierarchy
128 unsigned ppsHierarchy : 1; //2) SET if the object belongs to PPS
129 // Hierarchy
130 unsigned spsHierarchy : 1; //3) SET f the object belongs to SPS
131 // Hierarchy
132 unsigned evict : 1; //4) SET if the object is a platform or
133 // owner evict object. Platform-
134 // evict object belongs to PPS
135 // hierarchy, owner-evict object
136 // belongs to SPS or EPS hierarchy.
137 // This bit is also used to mark a
138 // completed sequence object so it
139 // will be flush when the
140 // SequenceComplete command succeeds.
141 unsigned primary : 1; //5) SET for a primary object
142 unsigned temporary : 1;
143 //6) SET for a temporary object
144 unsigned stClear : 1;
145 //7) SET for an stClear object
146 unsigned hmacSeq : 1;
147 //8) SET for an HMAC sequence object
148 unsigned hashSeq : 1;
149 //9) SET for a hash sequence object
150 unsigned eventSeq : 1;
151 //10) SET for an event sequence object
152 unsigned ticketSafe : 1;
153 //11) SET if a ticket is safe to create
154 // for hash sequence object
155 unsigned firstBlock : 1; //12) SET if the first block of hash
156 // data has been received. It
157 // works with ticketSafe bit
158 unsigned isParent : 1; //13) SET if the key has the proper
159 // attributes to be a parent key
160 unsigned privateExp : 1; //14) SET when the private exponent
161 // of an RSA key has been validated.
162 unsigned reserved : 1; //15) reserved bits. unused.
163} OBJECT_ATTRIBUTES;
164//
165//
166// OBJECT Structure
167//
168// An OBJECT structure holds the object public, sensitive, and meta-data associated. This structure is
169// implementation dependent. For this implementation, the structure is not optimized for space but rather for
170// clarity of the reference implementation. Other implementations may choose to overlap portions of the
171// structure that are not used simultaneously. These changes would necessitate changes to the source code
172// but those changes would be compatible with the reference implementation.
173//
174typedef struct
175{
176 // The attributes field is required to be first followed by the publicArea.
177 // This allows the overlay of the object structure and a sequence structure
178 OBJECT_ATTRIBUTES attributes; // object attributes
179 TPMT_PUBLIC publicArea; // public area of an object
180 TPMT_SENSITIVE sensitive; // sensitive area of an object
181#ifdef TPM_ALG_RSA
182 TPM2B_PUBLIC_KEY_RSA privateExponent; // Additional field for the private
183 // exponent of an RSA key.
184#endif
185 TPM2B_NAME qualifiedName; // object qualified name
186 TPMI_DH_OBJECT evictHandle; // if the object is an evict object,
187 // the original handle is kept here.
188 // The 'working' handle will be the
189 // handle of an object slot.
190 TPM2B_NAME name; // Name of the object name. Kept here
191 // to avoid repeatedly computing it.
192} OBJECT;
nagendra modadugu1d3ac312016-12-12 17:31:58 -0800193#ifdef EMBEDDED_MODE
194// This build time assert serves as a rudimentary check for changes
195// to the OBJECT structure (which is serialized to NVmem). Whenever
196// the OBJECT struct changes, NV_FORMAT_VERSION ought to be bumped.
197struct size_check { char a[sizeof(OBJECT) == 1536 ? 1 : -1]; };
198#endif
Vadim Bendebury56797522015-05-20 10:32:25 -0700199//
200//
201// HASH_OBJECT Structure
202//
203// This structure holds a hash sequence object or an event sequence object.
204// The first four components of this structure are manually set to be the same as the first four components of
205// the object structure. This prevents the object from being inadvertently misused as sequence objects
206// occupy the same memory as a regular object. A debug check is present to make sure that the offsets are
207// what they are supposed to be.
208//
209typedef struct
210{
211 OBJECT_ATTRIBUTES attributes; // The attributes of the HASH object
212 TPMI_ALG_PUBLIC type; // algorithm
213 TPMI_ALG_HASH nameAlg; // name algorithm
214 TPMA_OBJECT objectAttributes; // object attributes
215 // The data below is unique to a sequence object
216 TPM2B_AUTH auth; // auth for use of sequence
217 union
218 {
219 HASH_STATE hashState[HASH_COUNT];
220 HMAC_STATE hmacState;
221 } state;
222} HASH_OBJECT;
223//
224//
225// ANY_OBJECT
226//
227// This is the union for holding either a sequence object or a regular object.
228//
229typedef union
230{
231 OBJECT entity;
232 HASH_OBJECT hash;
233} ANY_OBJECT;
234//
235//
236// AUTH_DUP Types
237//
238// These values are used in the authorization processing.
239//
240typedef UINT32 AUTH_ROLE;
241#define AUTH_NONE ((AUTH_ROLE)(0))
242#define AUTH_USER ((AUTH_ROLE)(1))
243#define AUTH_ADMIN ((AUTH_ROLE)(2))
244#define AUTH_DUP ((AUTH_ROLE)(3))
245//
246//
247// Active Session Context
248//
249// Description
250//
251// The structures in this section define the internal structure of a session context.
252//
253// SESSION_ATTRIBUTES
254//
255// The attributes in the SESSION_ATTRIBUTES structure track the various properties of the session. It
256// maintains most of the tracking state information for the policy session. It is used within the SESSION
257// structure.
258//
259typedef struct
260{
261 unsigned isPolicy : 1; //1) SET if the session may only
262 // be used for policy
263 unsigned isAudit : 1; //2) SET if the session is used
264 // for audit
265 unsigned isBound : 1; //3) SET if the session is bound to
266 // with an entity.
267 // This attribute will be CLEAR if
268 // either isPolicy or isAudit is SET.
269 unsigned iscpHashDefined : 1;//4) SET if the cpHash has been defined
270 // This attribute is not SET unless
271 // 'isPolicy' is SET.
272 unsigned isAuthValueNeeded : 1;
273 //5) SET if the authValue is required
274 // for computing the session HMAC.
275 // This attribute is not SET unless
276 // isPolicy is SET.
277 unsigned isPasswordNeeded : 1;
278 //6) SET if a password authValue is
279 // required for authorization
280 // This attribute is not SET unless
281 // isPolicy is SET.
282 unsigned isPPRequired : 1; //7) SET if physical presence is
283 // required to be asserted when the
284 // authorization is checked.
285 // This attribute is not SET unless
286 // isPolicy is SET.
287 unsigned isTrialPolicy : 1; //8) SET if the policy session is
288 // created for trial of the policy's
289 // policyHash generation.
290 // This attribute is not SET unless
291 // isPolicy is SET.
292 unsigned isDaBound : 1; //9) SET if the bind entity had noDA
293 // CLEAR. If this is SET, then an
294 // auth failure using this session
295 // will count against lockout even
296 // if the object being authorized is
297 // exempt from DA.
298 unsigned isLockoutBound : 1; //10)SET if the session is bound to
299 // lockoutAuth.
300 unsigned requestWasBound : 1;//11) SET if the session is being used
301 // with the bind entity. If SET
302 // the authValue will not be use
303 // in the response HMAC computation.
304 unsigned checkNvWritten : 1; //12) SET if the TPMA_NV_WRITTEN
305 // attribute needs to be checked
306 // when the policy is used for
307 // authorization for NV access.
308 // If this is SET for any other
309 // type, the policy will fail.
310 unsigned nvWrittenState : 1; //13) SET if TPMA_NV_WRITTEN is
311 // required to be SET.
312} SESSION_ATTRIBUTES;
313//
314//
315// SESSION Structure
316//
317// The SESSION structure contains all the context of a session except for the associated contextID.
318//
319// NOTE: The contextID of a session is only relevant when the session context is stored off the TPM.
320//
321typedef struct
322{
323 TPM_ALG_ID authHashAlg; // session hash algorithm
324 TPM2B_NONCE nonceTPM; // last TPM-generated nonce for
325 // this session
326 TPMT_SYM_DEF symmetric; // session symmetric algorithm (if any)
327 TPM2B_AUTH sessionKey; // session secret value used for
328 // generating HMAC and encryption keys
329 SESSION_ATTRIBUTES attributes; // session attributes
330 TPM_CC commandCode; // command code (policy session)
331 TPMA_LOCALITY commandLocality; // command locality (policy session)
332 UINT32 pcrCounter; // PCR counter value when PCR is
333 // included (policy session)
334 // If no PCR is included, this
335 // value is 0.
336 UINT64 startTime; // value of TPMS_CLOCK_INFO.clock when
337 // the session was started (policy
338//
339 // session)
340 UINT64 timeOut; // timeout relative to
341 // TPMS_CLOCK_INFO.clock
342 // There is no timeout if this value
343 // is 0.
344 union
345 {
346 TPM2B_NAME boundEntity; // value used to track the entity to
347 // which the session is bound
348 TPM2B_DIGEST cpHash; // the required cpHash value for the
349 // command being authorized
350 } u1; // 'boundEntity' and 'cpHash' may
351 // share the same space to save memory
352 union
353 {
354 TPM2B_DIGEST auditDigest; // audit session digest
355 TPM2B_DIGEST policyDigest; // policyHash
356 } u2; // audit log and policyHash may
357 // share space to save memory
358} SESSION;
359//
360//
361// PCR
362//
363// PCR_SAVE Structure
364//
365// The PCR_SAVE structure type contains the PCR data that are saved across power cycles. Only the static
366// PCR are required to be saved across power cycles. The DRTM and resettable PCR are not saved. The
367// number of static and resettable PCR is determined by the platform-specific specification to which the TPM
368// is built.
369//
370typedef struct
371{
372#ifdef TPM_ALG_SHA1
373 BYTE sha1[NUM_STATIC_PCR][SHA1_DIGEST_SIZE];
374#endif
375#ifdef TPM_ALG_SHA256
376 BYTE sha256[NUM_STATIC_PCR][SHA256_DIGEST_SIZE];
377#endif
378#ifdef TPM_ALG_SHA384
379 BYTE sha384[NUM_STATIC_PCR][SHA384_DIGEST_SIZE];
380#endif
381#ifdef TPM_ALG_SHA512
382 BYTE sha512[NUM_STATIC_PCR][SHA512_DIGEST_SIZE];
383#endif
384#ifdef TPM_ALG_SM3_256
385 BYTE sm3_256[NUM_STATIC_PCR][SM3_256_DIGEST_SIZE];
386#endif
387 // This counter increments whenever the PCR are updated.
388 // NOTE: A platform-specific specification may designate
389 // certain PCR changes as not causing this counter
390 // to increment.
391 UINT32 pcrCounter;
392} PCR_SAVE;
393//
394//
395//
396// PCR_POLICY
397//
398// This structure holds the PCR policies, one for each group of PCR controlled by policy.
399//
400typedef struct
401{
402 TPMI_ALG_HASH hashAlg[NUM_POLICY_PCR_GROUP];
403 TPM2B_DIGEST a;
404 TPM2B_DIGEST policy[NUM_POLICY_PCR_GROUP];
405} PCR_POLICY;
406//
407//
408// PCR_AUTHVALUE
409//
410// This structure holds the PCR policies, one for each group of PCR controlled by policy.
411//
412typedef struct
413{
414 TPM2B_DIGEST auth[NUM_AUTHVALUE_PCR_GROUP];
415} PCR_AUTHVALUE;
416//
417//
418// Startup
419//
420// SHUTDOWN_NONE
421//
422// Part 2 defines the two shutdown/startup types that may be used in TPM2_Shutdown() and
423// TPM2_Starup(). This additional define is used by the TPM to indicate that no shutdown was received.
424//
425// NOTE: This is a reserved value.
426//
427#define SHUTDOWN_NONE (TPM_SU)(0xFFFF)
428//
429//
430// STARTUP_TYPE
431//
432// This enumeration is the possible startup types. The type is determined by the combination of
433// TPM2_ShutDown() and TPM2_Startup().
434//
435typedef enum
436{
437 SU_RESET,
438 SU_RESTART,
439 SU_RESUME
440} STARTUP_TYPE;
441//
442//
443// NV
444//
445// NV_RESERVE
446//
Mary Ruthven33f90592021-02-01 10:27:47 -0800447// This enumeration defines the list of the elements of a reserved portion of NV. This list includes all
Vadim Bendebury56797522015-05-20 10:32:25 -0700448// the pre-defined data that takes space in NV, either as persistent data or as state save data. The
449// enumerations are used as indexes into an array of offset values. The offset values then are used to index
450// into NV. This is method provides an imperfect analog to an actual NV implementation.
451//
452typedef enum
453{
454// Entries below mirror the PERSISTENT_DATA structure. These values are written
455// to NV as individual items.
456 // hierarchy
457 NV_DISABLE_CLEAR,
458 NV_OWNER_ALG,
459 NV_ENDORSEMENT_ALG,
460 NV_LOCKOUT_ALG,
461 NV_OWNER_POLICY,
462 NV_ENDORSEMENT_POLICY,
463 NV_LOCKOUT_POLICY,
464 NV_OWNER_AUTH,
465 NV_ENDORSEMENT_AUTH,
466 NV_LOCKOUT_AUTH,
467 NV_EP_SEED,
468 NV_SP_SEED,
469 NV_PP_SEED,
470 NV_PH_PROOF,
471 NV_SH_PROOF,
472 NV_EH_PROOF,
473 // Time
474 NV_TOTAL_RESET_COUNT,
475 NV_RESET_COUNT,
476 // PCR
477 NV_PCR_POLICIES,
478 NV_PCR_ALLOCATED,
479 // Physical Presence
480 NV_PP_LIST,
481 // Dictionary Attack
482 NV_FAILED_TRIES,
483 NV_MAX_TRIES,
484 NV_RECOVERY_TIME,
485 NV_LOCKOUT_RECOVERY,
486 NV_LOCKOUT_AUTH_ENABLED,
487 // Orderly State flag
488 NV_ORDERLY,
489 // Command Audit
490 NV_AUDIT_COMMANDS,
491 NV_AUDIT_HASH_ALG,
492 NV_AUDIT_COUNTER,
493 // Algorithm Set
494 NV_ALGORITHM_SET,
495 NV_FIRMWARE_V1,
496 NV_FIRMWARE_V2,
497// The entries above are in PERSISTENT_DATA. The entries below represent
498// structures that are read and written as a unit.
499// ORDERLY_DATA data structure written on each orderly shutdown
500 NV_ORDERLY_DATA,
501// STATE_CLEAR_DATA structure written on each Shutdown(STATE)
502 NV_STATE_CLEAR,
503// STATE_RESET_DATA structure written on each Shutdown(STATE)
504 NV_STATE_RESET,
Vadim Bendeburyc9e573a2018-12-11 15:19:59 -0800505 NV_RESERVE_LAST, // end of NV reserved data list
506
507 //
508 // "Pseudo" reserved objects made available to the platform, allowing it to
509 // access to NV RAM INDEX space and maxCount, which are not considered
510 // reserved objects, but located at fixed offset in the NVMEM cache.
511 //
512 NV_RAM_INDEX_SPACE = NV_RESERVE_LAST,
Vadim Bendeburyab480d62019-02-12 11:05:36 -0800513 NV_MAX_COUNTER,
514 NV_PSEUDO_RESERVE_LAST // End of the list including 'pseudo' elements.
515
Vadim Bendebury56797522015-05-20 10:32:25 -0700516} NV_RESERVE;
517//
518// NV_INDEX
519//
520// The NV_INDEX structure defines the internal format for an NV index. The indexData size varies
521// according to the type of the index. In this implementation, all of the index is manipulated as a unit.
522//
523typedef struct
524{
525 TPMS_NV_PUBLIC publicArea;
526 TPM2B_AUTH authValue;
527} NV_INDEX;
528//
529//
530// COMMIT_INDEX_MASK
531//
532// This is the define for the mask value that is used when manipulating the bits in the commit bit array. The
533// commit counter is a 64-bit value and the low order bits are used to index the commitArray. This mask
534// value is applied to the commit counter to extract the bit number in the array.
535//
536#ifdef TPM_ALG_ECC
537#define COMMIT_INDEX_MASK ((UINT16)((sizeof(gr.commitArray)*8)-1))
538#endif
539//
540//
541// RAM Global Values
542//
543// Description
544//
545// The values in this section are only extant in RAM. They are defined here and instanced in Global.c.
546//
547// g_rcIndex
548//
549// This array is used to contain the array of values that are added to a return code when it is a parameter-,
550// handle-, or session-related error. This is an implementation choice and the same result can be achieved
551// by using a macro.
552//
553extern const UINT16 g_rcIndex[15];
554//
555//
556// g_exclusiveAuditSession
557//
558// This location holds the session handle for the current exclusive audit session. If there is no exclusive
559// audit session, the location is set to TPM_RH_UNASSIGNED.
560//
561extern TPM_HANDLE g_exclusiveAuditSession;
562//
563//
564// g_time
565//
566// This value is the count of milliseconds since the TPM was powered up. This value is initialized at
567// _TPM_Init().
568//
569extern UINT64 g_time;
570//
571//
572// g_phEnable
573//
574// This is the platform hierarchy control and determines if the platform hierarchy is available. This value is
575// SET on each TPM2_Startup(). The default value is SET.
576//
577extern BOOL g_phEnable;
578// g_pceReConfig
579//
580// This value is SET if a TPM2_PCR_Allocate() command successfully executed since the last
581// TPM2_Startup(). If so, then the next shutdown is required to be Shutdown(CLEAR).
582//
583extern BOOL g_pcrReConfig;
584//
585//
586// g_DRTMHandle
587//
588// This location indicates the sequence object handle that holds the DRTM sequence data. When not used,
589// it is set to TPM_RH_UNASSIGNED. A sequence DRTM sequence is started on either _TPM_Init() or
590// _TPM_Hash_Start().
591//
592extern TPMI_DH_OBJECT g_DRTMHandle;
593//
594//
595// g_DrtmPreStartup
596//
597// This value indicates that an H-CRTM occurred after _TPM_Init() but before TPM2_Startup(). The define
598// for PRE_STARTUP_FLAG is used to add the g_DrtmPreStartup value to gp_orderlyState at shutdown.
599// This hack is to avoid adding another NV variable.
600//
601extern BOOL g_DrtmPreStartup;
602#define PRE_STARTUP_FLAG 0x8000
603//
604//
605// g_StartupLocality3
606//
607// This value indicates that a TPM2_Startup() occured at locality 3. Otherwise, it at locality 0. The define for
608// STARTUP_LOCALITY_3 is to indicate that the startup was not at locality 0. This hack is to avoid adding
609// another NV variable.
610//
611extern BOOL g_StartupLocality3;
612#define STARTUP_LOCALITY_3 0x4000
613//
614//
615// g_updateNV
616//
617// This flag indicates if NV should be updated at the end of a command. This flag is set to FALSE at the
618// beginning of each command in ExecuteCommand(). This flag is checked in ExecuteCommand() after the
619// detailed actions of a command complete. If the command execution was successful and this flag is SET,
620// any pending NV writes will be committed to NV.
621//
622extern BOOL g_updateNV;
623//
624//
625// g_clearOrderly
626//
627// This flag indicates if the execution of a command should cause the orderly state to be cleared. This flag
628// is set to FALSE at the beginning of each command in ExecuteCommand() and is checked in
629// ExecuteCommand() after the detailed actions of a command complete but before the check of
630// g_updateNV. If this flag is TRUE, and the orderly state is not SHUTDOWN_NONE, then the orderly state
631// in NV memory will be changed to SHUTDOWN_NONE.
632//
633extern BOOL g_clearOrderly;
634//
635//
636//
637// g_prevOrderlyState
638//
639// This location indicates how the TPM was shut down before the most recent TPM2_Startup(). This value,
640// along with the startup type, determines if the TPM should do a TPM Reset, TPM Restart, or TPM
641// Resume.
642//
643extern TPM_SU g_prevOrderlyState;
644//
645//
646// g_nvOk
647//
648// This value indicates if the NV integrity check was successful or not. If not and the failure was severe, then
649// the TPM would have been put into failure mode after it had been re-manufactured. If the NV failure was in
650// the area where the state-save data is kept, then this variable will have a value of FALSE indicating that a
651// TPM2_Startup(CLEAR) is required.
652//
653extern BOOL g_nvOk;
654//
655//
656// g_platformUnique
657//
658// This location contains the unique value(s) used to identify the TPM. It is loaded on every
659// _TPM2_Startup() The first value is used to seed the RNG. The second value is used as a vendor
660// authValue. The value used by the RNG would be the value derived from the chip unique value (such as
661// fused) with a dependency on the authorities of the code in the TPM boot path. The second would be
662// derived from the chip unique value with a dependency on the details of the code in the boot path. That is,
663// the first value depends on the various signers of the code and the second depends on what was signed.
664// The TPM vendor should not be able to know the first value but they are expected to know the second.
665//
666extern TPM2B_AUTH g_platformUniqueAuthorities; // Reserved for RNG
667extern TPM2B_AUTH g_platformUniqueDetails; // referenced by VENDOR_PERMANENT
668//
669//
670// Persistent Global Values
671//
672// Description
673//
674// The values in this section are global values that are persistent across power events. The lifetime of the
675// values determines the structure in which the value is placed.
676//
677// PERSISTENT_DATA
678//
679// This structure holds the persistent values that only change as a consequence of a specific Protected
680// Capability and are not affected by TPM power events (TPM2_Startup() or TPM2_Shutdown().
681//
682typedef struct
683{
684//*********************************************************************************
685// Hierarchy
686//*********************************************************************************
687// The values in this section are related to the hierarchies.
688 BOOL disableClear; // TRUE if TPM2_Clear() using
689 // lockoutAuth is disabled
690 // Hierarchy authPolicies
691 TPMI_ALG_HASH ownerAlg;
692 TPMI_ALG_HASH endorsementAlg;
693 TPMI_ALG_HASH lockoutAlg;
694 TPM2B_DIGEST ownerPolicy;
695 TPM2B_DIGEST endorsementPolicy;
696 TPM2B_DIGEST lockoutPolicy;
697 // Hierarchy authValues
698 TPM2B_AUTH ownerAuth;
699 TPM2B_AUTH endorsementAuth;
700 TPM2B_AUTH lockoutAuth;
701 // Primary Seeds
702 TPM2B_SEED EPSeed;
703 TPM2B_SEED SPSeed;
704 TPM2B_SEED PPSeed;
705 // Note there is a nullSeed in the state_reset memory.
706 // Hierarchy proofs
707 TPM2B_AUTH phProof;
708 TPM2B_AUTH shProof;
709 TPM2B_AUTH ehProof;
710 // Note there is a nullProof in the state_reset memory.
711//*********************************************************************************
712// Reset Events
713//*********************************************************************************
714// A count that increments at each TPM reset and never get reset during the life
715// time of TPM. The value of this counter is initialized to 1 during TPM
716// manufacture process.
717 UINT64 totalResetCount;
718// This counter increments on each TPM Reset. The counter is reset by
719// TPM2_Clear().
720 UINT32 resetCount;
721//*********************************************************************************
722// PCR
723//*********************************************************************************
724// This structure hold the policies for those PCR that have an update policy.
725// This implementation only supports a single group of PCR controlled by
726// policy. If more are required, then this structure would be changed to
727// an array.
728 PCR_POLICY pcrPolicies;
729// This structure indicates the allocation of PCR. The structure contains a
730// list of PCR allocations for each implemented algorithm. If no PCR are
731// allocated for an algorithm, a list entry still exists but the bit map
732// will contain no SET bits.
733 TPML_PCR_SELECTION pcrAllocated;
734//*********************************************************************************
735// Physical Presence
736//*********************************************************************************
737// The PP_LIST type contains a bit map of the commands that require physical
738// to be asserted when the authorization is evaluated. Physical presence will be
739// checked if the corresponding bit in the array is SET and if the authorization
740// handle is TPM_RH_PLATFORM.
741//
742// These bits may be changed with TPM2_PP_Commands().
743 BYTE ppList[((TPM_CC_PP_LAST - TPM_CC_PP_FIRST + 1) + 7)/8];
744//*********************************************************************************
745// Dictionary attack values
746//*********************************************************************************
747// These values are used for dictionary attack tracking and control.
748 UINT32 failedTries; // the current count of unexpired
749 // authorization failures
750 UINT32 maxTries; // number of unexpired authorization
751 // failures before the TPM is in
752 // lockout
753 UINT32 recoveryTime; // time between authorization failures
754 // before failedTries is decremented
755 UINT32 lockoutRecovery; // time that must expire between
756 // authorization failures associated
757 // with lockoutAuth
758 BOOL lockOutAuthEnabled; // TRUE if use of lockoutAuth is
759 // allowed
760//*****************************************************************************
761// Orderly State
762//*****************************************************************************
763// The orderly state for current cycle
764 TPM_SU orderlyState;
765//*****************************************************************************
766// Command audit values.
767//*****************************************************************************
Namyoon Woo65994902019-10-31 10:38:51 -0700768 BYTE auditComands[(MAX_CAP_CC_ALL + 7) / 8];
Vadim Bendebury56797522015-05-20 10:32:25 -0700769 TPMI_ALG_HASH auditHashAlg;
770 UINT64 auditCounter;
771//*****************************************************************************
772// Algorithm selection
773//*****************************************************************************
774//
775// The 'algorithmSet' value indicates the collection of algorithms that are
776// currently in used on the TPM. The interpretation of value is vendor dependent.
777 UINT32 algorithmSet;
778//*****************************************************************************
779// Firmware version
780//*****************************************************************************
781// The firmwareV1 and firmwareV2 values are instanced in TimeStamp.c. This is
782// a scheme used in development to allow determination of the linker build time
783// of the TPM. An actual implementation would implement these values in a way that
784// is consistent with vendor needs. The values are maintained in RAM for simplified
Mary Ruthven33f90592021-02-01 10:27:47 -0800785// access with a primary version in NV. These values are modified in a
Vadim Bendebury56797522015-05-20 10:32:25 -0700786// vendor-specific way.
787// g_firmwareV1 contains the more significant 32-bits of the vendor version number.
788// In the reference implementation, if this value is printed as a hex
789// value, it will have the format of yyyymmdd
790 UINT32 firmwareV1;
791// g_firmwareV1 contains the less significant 32-bits of the vendor version number.
792// In the reference implementation, if this value is printed as a hex
793// value, it will have the format of 00 hh mm ss
794 UINT32 firmwareV2;
795} PERSISTENT_DATA;
796extern PERSISTENT_DATA gp;
797//
798//
799// ORDERLY_DATA
800//
801// The data in this structure is saved to NV on each TPM2_Shutdown().
802//
803typedef struct orderly_data
804{
805//
806//*****************************************************************************
807// TIME
808//*****************************************************************************
809// Clock has two parts. One is the state save part and one is the NV part. The
810// state save version is updated on each command. When the clock rolls over, the
811// NV version is updated. When the TPM starts up, if the TPM was shutdown in and
812// orderly way, then the sClock value is used to initialize the clock. If the
813// TPM shutdown was not orderly, then the persistent value is used and the safe
814// attribute is clear.
815 UINT64 clock; // The orderly version of clock
816 TPMI_YES_NO clockSafe; // Indicates if the clock value is
817 // safe.
818//*********************************************************************************
819// DRBG
820//*********************************************************************************
821#ifdef _DRBG_STATE_SAVE
822 // This is DRBG state data. This is saved each time the value of clock is
823 // updated.
824 DRBG_STATE drbgState;
825#endif
826} ORDERLY_DATA;
827extern ORDERLY_DATA go;
828//
829//
830// STATE_CLEAR_DATA
831//
832// This structure contains the data that is saved on Shutdown(STATE). and restored on Startup(STATE).
833// The values are set to their default settings on any Startup(Clear). In other words the data is only
834// persistent across TPM Resume.
835// If the comments associated with a parameter indicate a default reset value, the value is applied on each
836// Startup(CLEAR).
837//
838typedef struct state_clear_data
839{
840//*****************************************************************************
841// Hierarchy Control
842//*****************************************************************************
843 BOOL shEnable; // default reset is SET
844 BOOL ehEnable; // default reset is SET
845 BOOL phEnableNV; // default reset is SET
846 TPMI_ALG_HASH platformAlg; // default reset is TPM_ALG_NULL
847 TPM2B_DIGEST platformPolicy; // default reset is an Empty Buffer
848 TPM2B_AUTH platformAuth; // default reset is an Empty Buffer
849//*****************************************************************************
850// PCR
851//*****************************************************************************
852// The set of PCR to be saved on Shutdown(STATE)
853 PCR_SAVE pcrSave; // default reset is 0...0
854// This structure hold the authorization values for those PCR that have an
855// update authorization.
856// This implementation only supports a single group of PCR controlled by
857// authorization. If more are required, then this structure would be changed to
858// an array.
859 PCR_AUTHVALUE pcrAuthValues;
860} STATE_CLEAR_DATA;
861extern STATE_CLEAR_DATA gc;
862//
863//
864//
865// State Reset Data
866//
867// This structure contains data is that is saved on Shutdown(STATE) and restored on the subsequent
868// Startup(ANY). That is, the data is preserved across TPM Resume and TPM Restart.
869// If a default value is specified in the comments this value is applied on TPM Reset.
870//
871typedef struct state_reset_data
872{
873//*****************************************************************************
874// Hierarchy Control
875//*****************************************************************************
876 TPM2B_AUTH nullProof; // The proof value associated with
877 // the TPM_RH_NULL hierarchy. The
878 // default reset value is from the RNG.
879 TPM2B_SEED nullSeed; // The seed value for the TPM_RN_NULL
880 // hierarchy. The default reset value
881 // is from the RNG.
882//*****************************************************************************
883// Context
884//*****************************************************************************
885// The 'clearCount' counter is incremented each time the TPM successfully executes
886// a TPM Resume. The counter is included in each saved context that has 'stClear'
887// SET (including descendants of keys that have 'stClear' SET). This prevents these
888// objects from being loaded after a TPM Resume.
889// If 'clearCount' at its maximum value when the TPM receives a Shutdown(STATE),
890// the TPM will return TPM_RC_RANGE and the TPM will only accept Shutdown(CLEAR).
891 UINT32 clearCount; // The default reset value is 0.
892 UINT64 objectContextID; // This is the context ID for a saved
893 // object context. The default reset
894 // value is 0.
895 CONTEXT_SLOT contextArray[MAX_ACTIVE_SESSIONS];
896 // This is the value from which the
897 // 'contextID' is derived. The
898 // default reset value is {0}.
899 CONTEXT_COUNTER contextCounter; // This array contains contains the
900 // values used to track the version
901 // numbers of saved contexts (see
902 // Session.c in for details). The
903 // default reset value is 0.
904//*****************************************************************************
905// Command Audit
906//*****************************************************************************
907// When an audited command completes, ExecuteCommand() checks the return
908// value. If it is TPM_RC_SUCCESS, and the command is an audited command, the
909// TPM will extend the cpHash and rpHash for the command to this value. If this
910// digest was the Zero Digest before the cpHash was extended, the audit counter
911// is incremented.
912 TPM2B_DIGEST commandAuditDigest; // This value is set to an Empty Digest
913 // by TPM2_GetCommandAuditDigest() or a
914 // TPM Reset.
915//*****************************************************************************
916// Boot counter
917//*****************************************************************************
918 UINT32 restartCount; // This counter counts TPM Restarts.
919 // The default reset value is 0.
920//
921//*********************************************************************************
922// PCR
923//*********************************************************************************
924// This counter increments whenever the PCR are updated. This counter is preserved
925// across TPM Resume even though the PCR are not preserved. This is because
926// sessions remain active across TPM Restart and the count value in the session
927// is compared to this counter so this counter must have values that are unique
928// as long as the sessions are active.
929// NOTE: A platform-specific specification may designate that certain PCR changes
930// do not increment this counter to increment.
931 UINT32 pcrCounter; // The default reset value is 0.
932#ifdef TPM_ALG_ECC
933//*****************************************************************************
934// ECDAA
935//*****************************************************************************
936 UINT64 commitCounter; // This counter increments each time
937 // TPM2_Commit() returns
938 // TPM_RC_SUCCESS. The default reset
939 // value is 0.
940 TPM2B_NONCE commitNonce; // This random value is used to compute
941 // the commit values. The default reset
942 // value is from the RNG.
943// This implementation relies on the number of bits in g_commitArray being a
944// power of 2 (8, 16, 32, 64, etc.) and no greater than 64K.
945 BYTE commitArray[16]; // The default reset value is {0}.
946#endif //TPM_ALG_ECC
947} STATE_RESET_DATA;
948extern STATE_RESET_DATA gr;
949//
950//
951// Global Macro Definitions
952//
953// This macro is used to ensure that a handle, session, or parameter number is only added if the response
954// code is FMT1.
955//
956#define RcSafeAddToResult(r, v) \
957 ((r) + (((r) & RC_FMT1) ? (v) : 0))
958//
959// This macro is used when a parameter is not otherwise referenced in a function. This macro is normally
960// not used by itself but is paired with a pAssert() within a #ifdef pAssert. If pAssert is not defined, then a
961// parameter might not otherwise be referenced. This macro uses the parameter from the perspective of the
962// compiler so it doesn't complain.
963//
964#define UNREFERENCED(a) ((void)(a))
965//
966//
967// Private data
968//
969#if defined SESSION_PROCESS_C || defined GLOBAL_C || defined MANUFACTURE_C
970//
971// From SessionProcess.c
972// The following arrays are used to save command sessions information so that the command
973// handle/session buffer does not have to be preserved for the duration of the command. These arrays are
974// indexed by the session index in accordance with the order of sessions in the session area of the
975// command.
976//
977// Array of the authorization session handles
978//
979extern TPM_HANDLE s_sessionHandles[MAX_SESSION_NUM];
980//
981// Array of authorization session attributes
982//
983extern TPMA_SESSION s_attributes[MAX_SESSION_NUM];
984//
985// Array of handles authorized by the corresponding authorization sessions; and if none, then
986// TPM_RH_UNASSIGNED value is used
987//
988extern TPM_HANDLE s_associatedHandles[MAX_SESSION_NUM];
989//
990// Array of nonces provided by the caller for the corresponding sessions
991//
992extern TPM2B_NONCE s_nonceCaller[MAX_SESSION_NUM];
993//
994// Array of authorization values (HMAC's or passwords) for the corresponding sessions
995//
996extern TPM2B_AUTH s_inputAuthValues[MAX_SESSION_NUM];
997//
998// Special value to indicate an undefined session index
999//
1000#define UNDEFINED_INDEX (0xFFFF)
1001//
1002// Index of the session used for encryption of a response parameter
1003//
1004extern UINT32 s_encryptSessionIndex;
1005//
1006// Index of the session used for decryption of a command parameter
1007//
1008extern UINT32 s_decryptSessionIndex;
1009//
1010// Index of a session used for audit
1011//
1012extern UINT32 s_auditSessionIndex;
1013//
1014// The cpHash for an audit session
1015//
1016extern TPM2B_DIGEST s_cpHashForAudit;
1017//
1018// The cpHash for command audit
1019//
1020#ifdef TPM_CC_GetCommandAuditDigest
1021extern TPM2B_DIGEST s_cpHashForCommandAudit;
1022#endif
1023//
1024// Number of authorization sessions present in the command
1025//
1026extern UINT32 s_sessionNum;
1027//
1028// Flag indicating if NV update is pending for the lockOutAuthEnabled or failedTries DA parameter
1029//
1030extern BOOL s_DAPendingOnNV;
1031#endif // SESSION_PROCESS_C
1032#if defined DA_C || defined GLOBAL_C || defined MANUFACTURE_C
1033//
1034// From DA.c
1035//
1036// This variable holds the accumulated time since the last time that failedTries was decremented. This value
1037// is in millisecond.
1038//
1039extern UINT64 s_selfHealTimer;
1040//
1041// This variable holds the accumulated time that the lockoutAuth has been blocked.
1042//
1043extern UINT64 s_lockoutTimer;
1044#endif // DA_C
1045#if defined NV_C || defined GLOBAL_C
1046//
1047// From NV.c
1048// List of pre-defined address of reserved data
1049//
1050extern UINT32 s_reservedAddr[NV_RESERVE_LAST];
1051//
1052// List of pre-defined reserved data size in byte
1053//
1054extern UINT32 s_reservedSize[NV_RESERVE_LAST];
1055//
1056// Size of data in RAM index buffer
1057//
1058extern UINT32 s_ramIndexSize;
1059//
1060// Reserved RAM space for frequently updated NV Index. The data layout in ram buffer is {NV_handle(),
1061// size of data, data} for each NV index data stored in RAM
1062//
1063extern BYTE s_ramIndex[RAM_INDEX_SPACE];
1064//
1065// Address of size of RAM index space in NV
1066//
1067extern UINT32 s_ramIndexSizeAddr;
1068//
1069// Address of NV copy of RAM index space
1070//
1071extern UINT32 s_ramIndexAddr;
1072//
1073// Address of maximum counter value; an auxiliary variable to implement NV counters
1074//
1075extern UINT32 s_maxCountAddr;
1076//
1077// Beginning of NV dynamic area; starts right after the s_maxCountAddr and s_evictHandleMapAddr
1078// variables
1079//
1080extern UINT32 s_evictNvStart;
1081//
1082// Beginning of NV dynamic area; also the beginning of the predefined reserved data area.
1083//
1084extern UINT32 s_evictNvEnd;
1085//
1086// NV availability is sampled as the start of each command and stored here so that its value remains
1087// consistent during the command execution
1088//
1089extern TPM_RC s_NvStatus;
1090#endif
1091#if defined OBJECT_C || defined GLOBAL_C
1092//
1093// From Object.c
1094//
1095// This type is the container for an object.
1096//
1097typedef struct
1098{
1099 BOOL occupied;
1100 ANY_OBJECT object;
1101} OBJECT_SLOT;
1102//
1103// This is the memory that holds the loaded objects.
1104//
1105extern OBJECT_SLOT s_objects[MAX_LOADED_OBJECTS];
1106#endif // OBJECT_C
1107#if defined PCR_C || defined GLOBAL_C
1108//
1109// From PCR.c
1110//
1111typedef struct
1112{
1113#ifdef TPM_ALG_SHA1
1114 // SHA1 PCR
1115 BYTE sha1Pcr[SHA1_DIGEST_SIZE];
1116#endif
1117#ifdef TPM_ALG_SHA256
1118 // SHA256 PCR
1119 BYTE sha256Pcr[SHA256_DIGEST_SIZE];
1120#endif
1121#ifdef TPM_ALG_SHA384
1122 // SHA384 PCR
1123 BYTE sha384Pcr[SHA384_DIGEST_SIZE];
1124#endif
1125#ifdef TPM_ALG_SHA512
1126 // SHA512 PCR
1127 BYTE sha512Pcr[SHA512_DIGEST_SIZE];
1128#endif
1129#ifdef TPM_ALG_SM3_256
1130 // SHA256 PCR
1131 BYTE sm3_256Pcr[SM3_256_DIGEST_SIZE];
1132#endif
1133} PCR;
1134typedef struct
1135{
1136 unsigned int stateSave : 1; // if the PCR value should be
1137 // saved in state save
1138 unsigned int resetLocality : 5; // The locality that the PCR
1139 // can be reset
1140 unsigned int extendLocality : 5; // The locality that the PCR
1141 // can be extend
1142} PCR_Attributes;
1143extern PCR s_pcrs[IMPLEMENTATION_PCR];
1144#endif // PCR_C
1145#if defined SESSION_C || defined GLOBAL_C
1146//
1147// From Session.c
1148// Container for HMAC or policy session tracking information
1149//
1150typedef struct
1151{
1152 BOOL occupied;
1153 SESSION session; // session structure
1154} SESSION_SLOT;
1155extern SESSION_SLOT s_sessions[MAX_LOADED_SESSIONS];
1156//
1157//
1158//
1159//
1160// The index in conextArray that has the value of the oldest saved session context. When no context is
1161// saved, this will have a value that is greater than or equal to MAX_ACTIVE_SESSIONS.
1162//
1163extern UINT32 s_oldestSavedSession;
1164//
1165// The number of available session slot openings. When this is 1, a session can't be created or loaded if the
1166// GAP is maxed out. The exception is that the oldest saved session context can always be loaded
1167// (assuming that there is a space in memory to put it)
1168//
1169extern int s_freeSessionSlots;
1170#endif // SESSION_C
1171//
1172// From Manufacture.c
1173//
1174extern BOOL g_manufactured;
1175#if defined POWER_C || defined GLOBAL_C
1176//
1177// From Power.c
1178// This value indicates if a TPM2_Startup() commands has been receive since the power on event. This
1179// flag is maintained in power simulation module because this is the only place that may reliably set this flag
1180// to FALSE.
1181//
1182extern BOOL s_initialized;
1183#endif // POWER_C
1184#if defined MEMORY_LIB_C || defined GLOBAL_C
1185//
1186// The s_actionOutputBuffer should not be modifiable by the host system until the TPM has returned a
1187// response code. The s_actionOutputBuffer should not be accessible until response parameter encryption,
1188// if any, is complete.
1189//
1190extern UINT32 s_actionInputBuffer[1024]; // action input buffer
1191extern UINT32 s_actionOutputBuffer[1024]; // action output buffer
1192extern BYTE s_responseBuffer[MAX_RESPONSE_SIZE];// response buffer
1193#endif // MEMORY_LIB_C
1194//
1195// From TPMFail.c
1196// This value holds the address of the string containing the name of the function in which the failure
1197// occurred. This address value isn't useful for anything other than helping the vendor to know in which file
1198// the failure occurred.
1199//
Stefan Reinauer0994ac92016-01-28 15:28:59 -08001200#ifndef EMBEDDED_MODE
Vadim Bendebury56797522015-05-20 10:32:25 -07001201extern jmp_buf g_jumpBuffer; // the jump buffer
Stefan Reinauer0994ac92016-01-28 15:28:59 -08001202#endif
Vadim Bendebury56797522015-05-20 10:32:25 -07001203extern BOOL g_inFailureMode; // Indicates that the TPM is in failure mode
1204extern BOOL g_forceFailureMode; // flag to force failure mode during test
1205#if defined TPM_FAIL_C || defined GLOBAL_C || 1
1206extern UINT32 s_failFunction;
1207extern UINT32 s_failLine; // the line in the file at which
1208 // the error was signaled
1209extern UINT32 s_failCode; // the error code used
1210#endif // TPM_FAIL_C
1211#endif // GLOBAL_H