Trusted Platform Module LibraryPart 4: Supporting Routines
Family “2.0”Level 00 Revision 01.64
December 24, 2020

Committee Draft

Contact: admin@trustedcomputinggroup.org

Work in ProgressThis document is an intermediate draft for comment
only and is subject to change without notice. Readers
should not design products based on this document.

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

TCG

Part 4: Supporting Routines

Trusted Platform Module Library

Licenses and Notices
Copyright Licenses:•

Trusted Computing Group (TCG) grants to the user of the source code in this specification (the“Source Code”) a worldwide, irrevocable, nonexclusive, royalty free, copyright license to reproduce,
create derivative works, distribute, display and perform the Source Code and derivative works
thereof, and to grant others the rights granted herein.

•

The TCG grants to the user of the other parts of the specification (other than the Source Code) therights to reproduce, distribute, display, and perform the specification solely for the purpose ofdeveloping products based on such documents.

Source Code Distribution Conditions:•

Redistributions of Source Code must retain the above copyright licenses, this list of conditions andthe following disclaimers.

•

Redistributions in binary form must reproduce the above copyright licenses, this list of conditions andthe following disclaimers in the documentation and/or other materials provided with the distribution.

Disclaimers:•

THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF
LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITHRESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES) THAT
MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE. Contact TCG
Administration (admin@trustedcomputinggroup.org) for information on specification licensing rights
available through TCG membership agreements.

•

THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED WARRANTIESWHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A
PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR NONINFRINGEMENT OFINTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY OTHERWISE ARISING OUT OF ANYPROPOSAL, SPECIFICATION OR SAMPLE.

•

Without limitation, TCG and its members and licensors disclaim all liability, including liability for
infringement of any proprietary rights, relating to use of information in this specification and to theimplementation of this specification, and TCG disclaims all liability for cost of procurement of
substitute goods or services, lost profits, loss of use, loss of data or any incidental, consequential,
direct, indirect, or special damages, whether under contract, tort, warranty or otherwise, arising in any
way out of use or reliance upon this specification or any information herein.

Any marks and brands contained herein are the property of their respective owners.

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page ii
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

CONTENTSCONTENTS .............................................................................................................................. 3
1

Scope ................................................................................................................................ 1

2

Terms and definitions ........................................................................................................ 1

3

Symbols and abbreviated terms ......................................................................................... 1

4

Automation ........................................................................................................................ 1
4.1 Configuration Parser ................................................................................................. 1
4.2 Structure Parser ....................................................................................................... 2
4.2.1
4.2.2

Introduction ....................................................................................................... 2
Unmarshaling Code Prototype ............................................................................ 2

4.2.3

Marshaling Code Function Prototypes ................................................................ 3

4.2.4

Table-driven Marshaling ..................................................................................... 4

4.3
4.4
4.5

Part 3 Parsing .......................................................................................................... 5
Function Prototypes .................................................................................................. 5
Portability ................................................................................................................. 5

5

Header Files ...................................................................................................................... 7
5.1 Introduction .............................................................................................................. 7
5.2 BaseTypes.h ............................................................................................................ 7
5.3 Capabilities.h ........................................................................................................... 8
5.4 CommandAttributeData.h .......................................................................................... 9
5.5 CommandAttributes.h .............................................................................................. 10
5.6 CommandDispatchData.h ........................................................................................ 11
5.7 Commands.h ........................................................................................................... 12
5.8 CompilerDependencies.h ......................................................................................... 13
5.9 Global.h .................................................................................................................. 14
5.10 GpMacros.h ............................................................................................................. 15
5.11 InternalRoutines.h ................................................................................................... 16
5.12 LibSupport.h ............................................................................................................ 17
5.13 MinMax.h ................................................................................................................ 17
5.14 NV.h ........................................................................................................................ 18
5.15 TPMB.h ................................................................................................................... 19
5.16 Tpm.h ..................................................................................................................... 20
5.17 TpmBuildSwitches.h ................................................................................................ 21
5.18 TpmError.h .............................................................................................................. 22
5.19 TpmTypes.h ............................................................................................................ 23
5.20 VendorString.h ........................................................................................................ 24
5.21 swap.h .................................................................................................................... 25
5.22 ACT.h ..................................................................................................................... 26

6

Main ................................................................................................................................. 27
6.1 Introduction ............................................................................................................. 27
6.2 ExecCommand.c ..................................................................................................... 27
6.3 CommandDispatcher.c ............................................................................................. 28
6.3.1

Introduction ...................................................................................................... 28

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page iii
December 24, 2020

Part 4: Supporting Routines

6.4

Trusted Platform Module Library

SessionProcess.c .................................................................................................... 29

7

Command Support Functions ............................................................................................ 30
7.1 Introduction ............................................................................................................. 30
7.2 Attestation Command Support (Attest_spt.c) ............................................................ 30
7.3 Context Management Command Support (Context_spt.c) ......................................... 31
7.4 Policy Command Support (Policy_spt.c) ................................................................... 32
7.5 NV Command Support (NV_spt.c) ............................................................................ 33
7.6 Object Command Support (Object_spt.c) .................................................................. 34
7.7 Encrypt Decrypt Support (EncryptDecrypt_spt.c) ...................................................... 35
7.8 ACT Support (ACT_spt.c) ........................................................................................ 36

8

Subsystem ....................................................................................................................... 37
8.1 CommandAudit.c ..................................................................................................... 37
8.2 DA.c ........................................................................................................................ 38
8.3 Hierarchy.c .............................................................................................................. 39
8.4 NvDynamic.c ........................................................................................................... 40
8.5 NvReserved.c .......................................................................................................... 41
8.6 Object.c .................................................................................................................. 42
8.7 PCR.c ..................................................................................................................... 43
8.8 PP.c ........................................................................................................................ 44
8.9 Session.c ................................................................................................................ 45
8.10 Time.c ..................................................................................................................... 46

9Support ............................................................................................................................ 47
9.1 AlgorithmCap.c ........................................................................................................ 47
9.2 Bits.c ....................................................................................................................... 48
9.3 CommandCodeAttributes.c ...................................................................................... 49
9.4 Entity.c .................................................................................................................... 50
9.5 Global.c .................................................................................................................. 51
9.6 Handle.c .................................................................................................................. 52
9.7 IoBuffers.c ............................................................................................................... 53
9.8 Locality.c ................................................................................................................. 54
9.9 Manufacture.c ......................................................................................................... 55
9.10 Marshal.c ................................................................................................................ 56
9.10.19.10.29.10.39.10.49.10.59.10.69.10.7

Introduction ...................................................................................................... 56
Unmarshal and Marshal a Value ........................................................................ 56
Unmarshal and Marshal a Union ....................................................................... 57
Unmarshal and Marshal a Structure .................................................................. 59
Unmarshal and Marshal an Array ...................................................................... 60
TPM2B Handling ............................................................................................... 62
Table Marshal Headers ..................................................................................... 62

9.10.8 Table Marshal Source ....................................................................................... 63

9.11
9.12
9.13
9.14
9.15
9.16
9.17
10

MathOnByteBuffers.c ............................................................................................... 64
Memory.c ................................................................................................................ 65
Power.c ................................................................................................................... 66
PropertyCap.c ......................................................................................................... 67
Response.c ............................................................................................................. 68
ResponseCodeProcessing.c .................................................................................... 69
TpmFail.c ................................................................................................................ 70

Cryptographic Functions ................................................................................................... 71

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page iv
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.1 Headers .................................................................................................................. 71
10.1.1 BnValues.h ....................................................................................................... 71
10.1.2 CryptEcc.h ........................................................................................................ 72
10.1.3 CryptHash.h ..................................................................................................... 73
10.1.4 CryptRand.h ..................................................................................................... 74
10.1.5 CryptRsa.h ....................................................................................................... 75
10.1.6 CryptTest.h ....................................................................................................... 76
10.1.7 HashTestData.h ................................................................................................ 77
10.1.8 KdfTestData.h .................................................................................................. 78
10.1.9 RsaTestData.h .................................................................................................. 79
10.1.10 SelfTest.h ......................................................................................................... 79
10.1.11 SupportLibraryFunctionPrototypes_fp.h ............................................................. 79
10.1.12 SymmetricTestData.h ........................................................................................ 80
10.1.13 SymmetricTest.h ............................................................................................... 81
10.1.14 EccTestData.h .................................................................................................. 82
10.1.15 CryptSym.h ....................................................................................................... 83
10.1.16 OIDs.h .............................................................................................................. 84
10.1.17 PRNG_TestVectors.h ........................................................................................ 84
10.1.18 TpmAsn1.h ....................................................................................................... 84
10.1.19 X509.h.............................................................................................................. 84
10.1.20 TpmAlgorithmDefines.h ..................................................................................... 84
10.2 Source .................................................................................................................... 85
10.2.1 AlgorithmTests.c ............................................................................................... 85
10.2.2 BnConvert.c ...................................................................................................... 86
10.2.3 BnMath.c .......................................................................................................... 87
10.2.4 BnMemory.c ..................................................................................................... 88
10.2.5 CryptCmac.c ..................................................................................................... 89
10.2.6 CryptUtil.c ........................................................................................................ 90
10.2.7 CryptSelfTest.c ................................................................................................. 91
10.2.8 CryptEccData.c ................................................................................................. 92
10.2.9 CryptDes.c ....................................................................................................... 93
10.2.10 CryptEccKeyExchange.c ................................................................................... 94
10.2.11 CryptEccMain.c ................................................................................................ 95
10.2.12 CryptEccSignature.c ......................................................................................... 96
10.2.13 CryptHash.c ...................................................................................................... 97
10.2.14 CryptPrime.c ..................................................................................................... 98
10.2.15 CryptPrimeSieve.c ............................................................................................ 99
10.2.16 CryptRand.c ................................................................................................... 100
10.2.17 CryptRsa.c ..................................................................................................... 101
10.2.18 CryptSmac.c ................................................................................................... 102
10.2.19 CryptSym.c ..................................................................................................... 103
10.2.20 PrimeData.c .................................................................................................... 104
10.2.21 RsaKeyCache.c .............................................................................................. 105
10.2.22 Ticket.c .......................................................................................................... 106
10.2.23 TpmAsn1.c ..................................................................................................... 107
10.2.24 X509_ECC.c ................................................................................................... 108
10.2.25 X509_RSA.c ................................................................................................... 109
10.2.26 X509_spt.c ..................................................................................................... 110
10.2.27 AC_spt.c ........................................................................................................ 111
10.2.28 CryptEccCrypt.c .............................................................................................. 112
Annex A (informative) Implementation Dependent ................................................................. 113
A.1 Introduction ........................................................................................................... 113
A.2 TpmProfile.h .......................................................................................................... 113
A.3 TpmSizeChecks.c .................................................................................................. 113
Annex B (informative) Library-Specific .................................................................................. 114
Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page v
December 24, 2020

Part 4: Supporting Routines

B.1
B.2

Trusted Platform Module Library

Introduction ........................................................................................................... 114
OpenSSL-Specific Files ......................................................................................... 115

B.2.1.
B.2.2.

Introduction .................................................................................................... 115
Header Files ................................................................................................... 115

B.2.3.

Source Files ................................................................................................... 118

Annex C (informative) Simulation Environment ...................................................................... 121
C.1 Introduction ........................................................................................................... 121
C.2 Cancel.c ................................................................................................................ 121
C.3 Clock.c .................................................................................................................. 122
C.4 Entropy.c ............................................................................................................... 123
C.5 LocalityPlat.c ......................................................................................................... 124
C.6 NVMem.c .............................................................................................................. 125
C.7 PowerPlat.c ........................................................................................................... 126
C.8 PlatformData.h ...................................................................................................... 127
C.9 PlatformData.c ...................................................................................................... 128
C.10 PPPlat.c ................................................................................................................ 129
C.11 RunCommand.c ..................................................................................................... 130
C.12 Unique.c ................................................................................................................ 131
C.13 DebugHelpers.c ..................................................................................................... 132
C.14 Platform.h ............................................................................................................. 133
C.15 PlatformACT.h ....................................................................................................... 134
C.16 PlatformACT.c ....................................................................................................... 135
C.17 PlatformClock.h ..................................................................................................... 136
Annex D (informative) Remote Procedure Interface ............................................................... 137
D.1 Introduction ........................................................................................................... 137
D.2 TpmTcpProtocol.h ................................................................................................. 138
D.3 TcpServer.c ........................................................................................................... 139
D.4 TPMCmdp.c .......................................................................................................... 140
D.5 TPMCmds.c ........................................................................................................... 141

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page vi
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

Trusted Platform Module LibraryPart 4: Supporting Routines
1

Scope

This part contains C code that describes the algorithms and methods used by the command code in TPM
2.0 Part 3. The code in this document augments TPM 2.0 Part 2 and TPM 2.0 Part 3 to provide acomplete description of a TPM, including the supporting framework for the code that performs thecommand actions.
Any TPM 2.0 Part 4 code may be replaced by code that provides similar results when interfacing to theaction code in TPM 2.0 Part 3. The behavior of code in this document that is not included in an annex isnormative, as observed at the interfaces with TPM 2.0 Part 3 code. Code in an annex is provided forcompleteness, that is, to allow a full implementation of the specification from the provided code.
The code in parts 3 and 4 is written to define the behavior of a compliant TPM. In some cases (e.g.,
firmware update), it is not possible to provide a compliant implementation. In those cases, any
implementation provided by the vendor that meets the general description of the function provided in TPM
2.0 Part 3 would be compliant.
The code in parts 3 and 4 is not written to meet any particular level of conformance nor does thisspecification require that a TPM meet any particular level of conformance.

2

Terms and definitions

For the purposes of this document, the terms and definitions given in TPM 2.0 Part 1 apply.

3

Symbols and abbreviated terms

For the purposes of this document, the symbols and abbreviated terms given in TPM 2.0 Part 1 apply.

4

Automation

TPM 2.0 Part 2 and 3 are constructed so that they can be processed by an automated parser. For
example, TPM 2.0 Part 2 can be processed to generate header file contents such as structures, typedefs,
and enums. TPM 2.0 Part 3 can be processed to generate command and response marshaling andunmarshaling code.
The automated processor is not provided by the TCG. It was used to generate the Microsoft Visual Studio
TPM simulator files. These files are not specification reference code, but rather design examples.
The automation produces TPM_Types.h, a header representing TPM 2.0 Part 2. It also produces, foreach major clause of Part 4, a header of the form _fp.h with the function prototypes.
EXAMPLE

4.1

The header file for SessionProcess.c is SessionProcess_fp.h.

Configuration Parser

The TPM configuration is largely defined by TpmProfiles.h. This file may be edited in order to change thealgorithms and commands supported by a TPM implementation.
A parser exists to process a Word document that defines the TPM configuration. This parser is used tocreate TpmProfiles.h.

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 1
December 24, 2020

Part 4: Supporting Routines

4.2

Structure Parser

4.2.1

Introduction

Trusted Platform Module Library

The program that processes the tables in TPM 2.0 Part 2 is called "The TPM 2.0 Part 2 Structure Parser."NOTE

A Perl script was used to parse the tables in TPM 2.0 Part 2 to produce the header files and unmarshaling codein for the reference implementation.

The TPM 2.0 Part 2 Structure Parser takes as input the files produced by the TPM 2.0 Part 2
Configuration Parser and the same TPM 2.0 Part 2 specification that was used as input to the TPM 2.0
Part 2 Configuration Parser. The TPM 2.0 Part 2 Structure Parser will generate all of the C structure
constant definitions that are required by the TPM interface. Additionally, the parser will generate
unmarshaling code for all structures passed to the TPM and marshaling code for structures passed fromthe TPM.
The unmarshaling code produced by the parser uses the prototypes defined below. The unmarshalingcode will perform validations of the data to ensure that it is compliant with the limitations on the data
imposed by the structure definition and use the response code provided in the table if not.
EXAMPLE:The definition for a TPMI_RH_PROVISION indicates that the primitive data type is a TPM_HANDLE and theonly allowed values are TPM_RH_OWNER and TPM_RH_PLATFORM. The definition also indicates that the
TPM shall indicate TPM_RC_HANDLE if the input value is not none of these values. The unmarshaling code
will validate that the input value has one of those allowed values and return TPM_RC_HANDLE if not.

The clauses below describe the function prototypes for the marshaling and unmarshaling code that is
automatically generated by the TPM 2.0 Part 2 Structure Parser. These prototypes are described here as
the unmarshaling and marshaling of various types occurs in places other than when the command isbeing parsed or the response is being built. The prototypes and the description of the interface areintended to aid in the comprehension of the code that uses these auto-generated routines.
4.2.2

Unmarshaling Code Prototype

4.2.2.1

Simple Types and Structures

The general form for the unmarshaling code for a simple type or a structure is:
TPM_RC TYPE_Unmarshal(TYPE *target, BYTE **buffer, INT32 *size);

Where:
TYPE

name of the data type or structure*targetlocation in the TPM memory into which the data from **buffer is placed**buffer

location in input buffer containing the most significant octet (MSO) of*target*size

number of octets remaining in **buffer

When the data is successfully unmarshaled, the called routine will return TPM_RC_SUCCESS.
Otherwise, it will return a Format-One response code (see TPM 2.0 Part 2).
If the data is successfully unmarshaled, *buffer is advanced point to the first octet of the next parameterin the input buffer and size is reduced by the number of octets removed from the buffer.
When the data type is a simple type, the parser will generate code that will unmarshal the underlying type
and then perform checks on the type as indicated by the type definition.
When the data type is a structure, the parser will generate code that unmarshals each of the structureelements in turn and performs any additional parameter checks as indicated by the data type.
Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 2
December 24, 2020

Part 4: Supporting Routines

4.2.2.2

Trusted Platform Module Library

Union Types

When a union is defined, an extra parameter is defined for the unmarshaling code. This parameter is theselector for the type. The unmarshaling code for the union will unmarshal the type indicated by theselector.
The function prototype for a union has the form:
TPM_RC TYPE_Unmarshal(TYPE *target, BYTE **buffer, INT32 *size, UINT32 selector);

where:
TYPE

name of the union type or structure*targetlocation in the TPM memory into which the data from **buffer is placed**buffer

location in input buffer containing the most significant octet (MSO) of*target*size

number of octets remaining in **buffer

selector

union selector that determines what will be unmarshaled into *target

4.2.2.3

Null Types

In some cases, the structure definition allows an optional “null” value. The “null” value allows the use of
the same C type for the entity even though it does not always have the same members.
For example, the TPMI_ALG_HASH data type is used in many places. In some cases, TPM_ALG_NULL
is permitted and in some cases it is not. If two different data types had to be defined, the interfaces andcode would become more complex because of the number of cast operations that would be necessary.
Rather than encumber the code, the “null” value is defined and the unmarshaling code is given a flag toindicate if this instance of the type accepts the “null” parameter or not. When the data type has a “null”
value, the function prototype is
TPM_RC TYPE_Unmarshal(TYPE *target, BYTE **buffer, INT32 *size, BOOL flag);The parser detects when the type allows a “null” value and will always include flag in any call tounmarshal that type. flag TRUE indicates that null is accepted.
4.2.2.4

ArraysAny data type may be included in an array. The function prototype use to unmarshal an array for a TYPE is
TPM_RC TYPE_Array_Unmarshal(TYPE *target, BYTE **buffer, INT32 *size,INT32 count);

The generated code for an array uses a count-limited loop within which it calls the unmarshaling code for
TYPE.
4.2.3
4.2.3.1

Marshaling Code Function Prototypes
Simple Types and Structures

The general form for the marshaling code for a simple type or a structure is:
UINT16 TYPE_Marshal(TYPE *source, BYTE **buffer, INT32 *size);

Where:
Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 3
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

TYPE

name of the data type or structure*sourcelocation in the TPM memory containing the value that is to be marshaledin to the designated buffer**buffer

location in the output buffer where the first octet of the TYPE is to beplaced*size

number of octets remaining in **buffer.

If buffer is a NULL pointer, then no data is marshaled, but the routine will compute and return the sizeof the memory required to marshal the indicated type. *size is not changed.
If buffer is not a NULL pointer, data is marshaled, *buffer is advanced to point to the first octet of thenext location in the output buffer, and the called routine will return the number of octets marshaled into**buffer. This occurs even if size is a NULL pointer. If size is a not NULL pointer *size is reduced by
the number of octets placed in the buffer.
When the data type is a simple type, the parser will generate code that will marshal the underlying type.
The presumption is that the TPM internal structures are consistent and correct so the marshaling code
does not validate that the data placed in the buffer has a permissible value. The presumption is also thatthe size is sufficient for the source being marshaled.
When the data type is a structure, the parser will generate code that marshals each of the structureelements in turn.
4.2.3.2

Union Types

An extra parameter is defined for the marshaling function of a union. This parameter is the selector for thetype. The marshaling code for the union will marshal the type indicated by the selector.
The function prototype for a union has the form:
UINT16 TYPE_Marshal(TYPE *source, BYTE **buffer, INT32 *size, UINT32 selector);

The parameters have a similar meaning as those in 4.2.2.2 but the data movement is from source tobuffer.
4.2.3.3

ArraysAny type may be included in an array. The function prototype use to unmarshal an array is:
UINT16 TYPE_Array_Marshal(TYPE *source, BYTE **buffer, INT32 *size, INT32 count);

The generated code for an array uses a count-limited loop within which it calls the marshaling code for
TYPE.
4.2.4

Table-driven Marshaling

The most recent versions of the TPM code includes the option to use table-driven marshaling rather thatthe procedural marshaling described in previous clauses in 4.2.2. The structure and processing of thiscode is complex and is provided in the code.

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 4
December 24, 2020

Part 4: Supporting Routines

4.3

Trusted Platform Module LibraryPart 3 Parsing

The Command / Response tables in Part 3 of this specification are processed by scripts to produce thecommand-specific data structures used by functions in this TPM 2.0 Part 4. They are:•CommandAttributeData.h -- This file contains the command attributes reported by
TPM2_GetCapability.

•

CommandAttributes.h – This file contains the definition of command attributes that are extracted by
the parsing code. The file mainly exists to ensure that the parsing code and the function code areusing the same attributes.

•

CommandDispatchData.h – This file contains the data definitions for the table driven version of thecommand dispatcher.

Part 3 parsing also produces special function prototype files as described in 4.4.
4.4

Function Prototypes

For functions that have entry definitions not defined by Part 3 tables. a script is used to extracts function
prototypes from the code. For each .c file that is not in Part 3, a file with the same name is created with asuffix of _fp.h. For example, the function prototypes for Create.c will be placed in a file called Create_fp.h.
The _fp.h is added because some files have two types of associated headers: the one containing thefunction prototypes for the file and another containing definitions that are specific to that file.
In some cases, a function will be replaced by a macro. The macro is defined in the .c file and extracted by
the function prototype processor. A special comment tag (“//%”) is used to indicate that the line is to be
included in the function prototype file. If the “//%” tag occurs at the start of the line, it is deleted. If it occurs
later in the line, it is preserved. Removing the “//%/ at the start of the line allows the macro to be placed in
the .c file with the tag as a prefix, and then show up in the _fp.h file as the actual macro. This allows thecode that includes that function prototype code to use the appropriate macro.
For files that that contain the command actions, a special _fp.h file is created from the tables in Part 3.
These files contain:•

the definition of the input and output structure of the function;•

definition of command-specific return code modifiers (parameter identifiers); and•the function prototype for the command action function.

Create_fp.h (shown below) is prototypical of the command _fp.h files.
[[create_fp_h]]
4.5Portability

Where reasonable, the code is written to be portable. There are a few known cases where the code is not
portable. Specifically, the handling of bit fields will not always be portable. The bit fields are marshaledand unmarshaled as a simple element of the underlying type. For example, a TPMA_SESSION is defined
as a bit field in an octet (BYTE). When sent on the interface a TPMA_SESSION will occupy one octet.
When unmarshaled, it is unmarshaled as a UINT8. The ramifications of this are that a TPMA_SESSION
will occupy the 0th octet of the structure in which it is placed regardless of the size of the structure.
Many compilers will pad a bit field to some "natural" size for the processor, often 4 octets, meaning thatsizeof(TPMA_SESSION) would return 4 rather than 1 (the canonical size of a TPMA_SESSION).
For a little endian machine, padding of bit fields should have little consequence since the 0th octet always
contains the 0th bit of the structure no matter how large the structure. However, for a big endian machine,
the 0th bit will be in the highest numbered octet. When unmarshaling a TPMA_SESSION, the current

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 5
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

unmarshaling code will place the input octet at the 0th octet of the TPMA_SESSION. Since the 0th octet ismost significant octet, this has the effect of shifting all the session attribute bits left by 24 places.
As a consequence, someone implementing on a big endian machine should do one of two things:
allocate all structures as packed to a byte boundary (this may not be possible if the processor does
not handle unaligned accesses); or
modify the code that manipulates bit fields that are not defined as being the alignment size of thesystem.
For many RISC processors, option #2 would be the only choice. This is may not be a terribly daunting
task since only two attribute structures are not 32-bits (TPMA_SESSION and TPMA_LOCALITY).

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 6
December 24, 2020

Part 4: Supporting Routines

5
5.1

Trusted Platform Module Library

Header Files
Introduction

The files in this clause are used to define values that are used in multiple parts of the specification andare not confined to a single module.
5.2

BaseTypes.h[[BaseTypes_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 7
December 24, 2020

Part 4: Supporting Routines

5.3

Trusted Platform Module Library

Capabilities.h

This file contains defines for the number of capability values that will fit into the largest data buffer.
These defines are used in various function in the "support" and the "subsystem" code groups. A modulethat supports a type that is returned by a capability will have a function that returns the capabilities of thetype.
EXAMPLE

PCR.c contains PCRCapGetHandles() and PCRCapGetProperties().

[[Capabilities_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 8December 24, 2020

Part 4: Supporting Routines

5.4

Trusted Platform Module Library

CommandAttributeData.h[[CommandAttributeData_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 9December 24, 2020

Part 4: Supporting Routines

5.5

Trusted Platform Module Library

CommandAttributes.h[[CommandAttributes_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 10December 24, 2020

Part 4: Supporting Routines

5.6

Trusted Platform Module Library

CommandDispatchData.h[[CommandDispatchData_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 11
December 24, 2020

Part 4: Supporting Routines

5.7

Trusted Platform Module Library

Commands.h[[Commands_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 12
December 24, 2020

Part 4: Supporting Routines

5.8

Trusted Platform Module LibraryCompilerDependencies.h[[CompilerDependencies_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 13
December 24, 2020

Part 4: Supporting Routines

5.9

Trusted Platform Module Library

Global.h[[Global_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 14
December 24, 2020

Part 4: Supporting Routines

5.10

Trusted Platform Module Library

GpMacros.h[[GpMacros_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 15
December 24, 2020

Part 4: Supporting Routines

5.11

Trusted Platform Module Library

InternalRoutines.h[[InternalRoutines_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 16
December 24, 2020

Part 4: Supporting Routines

5.12

Trusted Platform Module Library

LibSupport.h[[LibSupport_h]]
5.13

MinMax.h[[MinMax_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 17
December 24, 2020

Part 4: Supporting Routines

5.14

Trusted Platform Module Library

NV.h[[NV_h]]Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 18
December 24, 2020

Part 4: Supporting Routines

5.15

Trusted Platform Module Library

TPMB.h[[TPMB_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 19
December 24, 2020

Part 4: Supporting Routines

5.16

Trusted Platform Module Library

Tpm.h[[Tpm_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 20
December 24, 2020

Part 4: Supporting Routines

5.17

Trusted Platform Module Library

TpmBuildSwitches.h[[TpmBuildSwitches_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 21
December 24, 2020

Part 4: Supporting Routines

5.18

Trusted Platform Module Library

TpmError.h[[TpmError_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 22
December 24, 2020

Part 4: Supporting Routines

5.19

Trusted Platform Module Library

TpmTypes.h[[TpmTypes_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 23
December 24, 2020

Part 4: Supporting Routines

5.20

Trusted Platform Module Library

VendorString.h[[VendorString_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 24
December 24, 2020

Part 4: Supporting Routines

5.21

Trusted Platform Module Libraryswap.h[[swap_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 25
December 24, 2020

Part 4: Supporting Routines

5.22

Trusted Platform Module Library

ACT.h[[ACT_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 26
December 24, 2020

Part 4: Supporting Routines

6
6.1

Trusted Platform Module Library

Main
Introduction

The files in this clause are the main processing blocks for the TPM. ExecuteCommand.c contains theentry point into the TPM code and the parsing of the command header. SessionProcess.c handles theparsing of the session area and the authorization checks, and CommandDispatch.c does the parameter
unmarshaling and command dispatch.
6.2

ExecCommand.c[[ExecCommand]]Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 27
December 24, 2020

Part 4: Supporting Routines

6.3

Trusted Platform Module Library

CommandDispatcher.c

6.3.1

Introduction

CommandDispatcher() performs the following operations:•

unmarshals command parameters from the input buffer;

NOTE 1

Unlike other unmarshaling functions, parmBufferStart does not advance but parmBufferSize is reduced.

•

invokes the function that performs the command actions;•

marshals the returned handles, if any; and•marshals the returned parameters, if any, into the output buffer putting in the parameterSize field if
authorization sessions are present.

NOTE 2The output buffer is the return from the MemoryGetResponseBuffer() function. It includes the header, handles,
response parameters, and authorization area. respParmSize is the response parameter size, and does notinclude the header, handles, or authorization area.

NOTE 3

The reference implementation is permitted to do compare operations over a union as a byte array. Therefore,
the command parameter in structure must be initialized (e.g., zeroed) before unmarshaling so that the compare
operation is valid in cases where some bytes are unused.

[[CommandDispatcher]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 28
December 24, 2020

Part 4: Supporting Routines

6.4

Trusted Platform Module Library

SessionProcess.c[[SessionProcess]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 29
December 24, 2020

Part 4: Supporting Routines

7
7.1

Trusted Platform Module Library

Command Support Functions
Introduction

This clause contains support routines that are called by the command action code in TPM 2.0 Part 3. The
functions are grouped by the command group that is supported by the functions.
7.2

Attestation Command Support (Attest_spt.c)[[Attest_spt]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 30
December 24, 2020

Part 4: Supporting Routines

7.3

Trusted Platform Module LibraryContext Management Command Support (Context_spt.c)[[Context_spt]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 31
December 24, 2020

Part 4: Supporting Routines

7.4

Trusted Platform Module Library

Policy Command Support (Policy_spt.c)[[Policy_spt]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 32
December 24, 2020

Part 4: Supporting Routines

7.5

Trusted Platform Module Library

NV Command Support (NV_spt.c)[[NV_spt]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 33
December 24, 2020

Part 4: Supporting Routines

7.6

Trusted Platform Module Library

Object Command Support (Object_spt.c)[[Object_spt]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 34
December 24, 2020

Part 4: Supporting Routines

7.7

Trusted Platform Module Library

Encrypt Decrypt Support (EncryptDecrypt_spt.c)[[EncryptDecrypt_spt]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 35
December 24, 2020

Part 4: Supporting Routines

7.8

Trusted Platform Module Library

ACT Support (ACT_spt.c)[[ACT_spt]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 36
December 24, 2020

Part 4: Supporting Routines

8
8.1

Trusted Platform Module Library

Subsystem
CommandAudit.c[[CommandAudit]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 37
December 24, 2020

Part 4: Supporting Routines

8.2

Trusted Platform Module Library

DA.c[[DA]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 38
December 24, 2020

Part 4: Supporting Routines

8.3

Trusted Platform Module Library

Hierarchy.c[[Hierarchy]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 39
December 24, 2020

Part 4: Supporting Routines

8.4

Trusted Platform Module Library

NvDynamic.c[[NVDynamic]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 40
December 24, 2020

Part 4: Supporting Routines

8.5

Trusted Platform Module Library

NvReserved.c[[NVReserved]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 41
December 24, 2020

Part 4: Supporting Routines

8.6

Trusted Platform Module Library

Object.c[[Object]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 42
December 24, 2020

Part 4: Supporting Routines

8.7

Trusted Platform Module Library

PCR.c[[PCR]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 43
December 24, 2020

Part 4: Supporting Routines

8.8

Trusted Platform Module Library

PP.c[[PP]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 44
December 24, 2020

Part 4: Supporting Routines

8.9

Trusted Platform Module Library

Session.c[[Session]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 45
December 24, 2020

Part 4: Supporting Routines

8.10

Trusted Platform Module Library

Time.c[[Time]]Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 46
December 24, 2020

Part 4: Supporting Routines

9
9.1

Trusted Platform Module Library

SupportAlgorithmCap.c[[AlgorithmCap]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 47
December 24, 2020

Part 4: Supporting Routines

9.2

Trusted Platform Module Library

Bits.c[[Bits]]Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 48
December 24, 2020

Part 4: Supporting Routines

9.3

Trusted Platform Module Library

CommandCodeAttributes.c[[CommandCodeAttributes]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 49
December 24, 2020

Part 4: Supporting Routines

9.4

Trusted Platform Module Library

Entity.c[[Entity]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 50
December 24, 2020

Part 4: Supporting Routines

9.5

Trusted Platform Module Library

Global.c[[Global]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 51
December 24, 2020

Part 4: Supporting Routines

9.6

Trusted Platform Module LibraryHandle.c[[Handle]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 52
December 24, 2020

Part 4: Supporting Routines

9.7

Trusted Platform Module Library

IoBuffers.c[[IoBuffers]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 53
December 24, 2020

Part 4: Supporting Routines

9.8

Trusted Platform Module Library

Locality.c[[Locality]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 54
December 24, 2020

Part 4: Supporting Routines

9.9

Trusted Platform Module Library

Manufacture.c[[Manufacture]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 55
December 24, 2020

Part 4: Supporting Routines

9.10

Trusted Platform Module Library

Marshal.c

9.10.1Introduction

This file contains the marshaling and unmarshaling code.
The marshaling and unmarshaling code and function prototypes are not listed, as the code is repetitive,
long, and not very useful to read. Examples of a few unmarshaling routines are provided. Most of theothers are similar.
Depending on the table header flags, a type will have an unmarshaling routine and a marshaling routine
The table header flags that control the generation of the unmarshaling and marshaling code are delimited
by angle brackets ("<>") in the table header. If no brackets are present, then both unmarshaling andmarshaling code is generated (i.e., generation of both marshaling and unmarshaling code is the default).
9.10.2

Unmarshal and Marshal a Value

In TPM 2.0 Part 2, a TPMI_DI_OBJECT is defined by this table:
Table xxx — Definition of (TPM_HANDLE) TPMI_DH_OBJECT Type
ValuesComments{TRANSIENT_FIRST:TRANSIENT_LAST}

allowed range for transient objects{PERSISTENT_FIRST:PERSISTENT_LAST}

allowed range for persistent objects+TPM_RH_NULL

the null handle#TPM_RC_VALUEThis generates the following unmarshaling code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

TPM_RC
TPMI_DH_OBJECT_Unmarshal(TPMI_DH_OBJECT *target, BYTE **buffer, INT32 *size,
BOOL flag){
TPM_RC
result;result = TPM_HANDLE_Unmarshal((TPM_HANDLE *)target, buffer, size);if(result != TPM_RC_SUCCESS)
return result;if(*target == TPM_RH_NULL){
if(flag)
return TPM_RC_SUCCESS;
else
return TPM_RC_VALUE;}
if(((*target < TRANSIENT_FIRST) || (*target > TRANSIENT_LAST))&&((*target < PERSISTENT_FIRST) || (*target > PERSISTENT_LAST)))
return TPM_RC_VALUE;
return TPM_RC_SUCCESS;}

and the following marshaling code:
NOTE

1
2

The marshaling code does not do parameter checking, as the TPM is the source of the marshaling data.

UINT16
TPMI_DH_OBJECT_Marshal(TPMI_DH_OBJECT *source, BYTE **buffer, INT32 *size)

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 56
December 24, 2020

Part 4: Supporting Routines
3
4
5

Trusted Platform Module Library{
return UINT32_Marshal((UINT32 *)source, buffer, size);}An additional script is used to do the work that might be done by a linker or globally optimizing compiler. It
searches for functions like TPMI_DH_OBJECT_Marshal() that do nothing but call another function andreplaces the function with a #define.
6
7#define TPMI_DH_OBJECT_Marshal(source, buffer, size)
UINT32_Marshal((UINT32 *)source, buffer, size)\

When replacing the function with a #define, the #define is placed in Marshal_fp.h and the function body isremoved from Marshal.c.
9.10.3

Unmarshal and Marshal a Union

In TPM 2.0 Part 2, a TPMU_PUBLIC_PARMS union is defined by:
Table xxx — Definition of TPMU_PUBLIC_PARMS Union <IN/OUT, S>
ParameterType

Selector

Description

keyedHash

TPMS_KEYEDHASH_PARMS

TPM_ALG_KEYEDHASH

sign | encrypt | neither

symDetail

TPMT_SYM_DEF_OBJECT

TPM_ALG_SYMCIPHER

a symmetric block cipher

rsaDetail

TPMS_RSA_PARMS

TPM_ALG_RSA

decrypt + sign

eccDetail

TPMS_ECC_PARMS

TPM_ALG_ECC

decrypt + sign

asymDetail

TPMS_ASYM_PARMS

common scheme structure
for RSA and ECC keys

NOTE The Description column indicates which of TPMA_OBJECT.decrypt or TPMA_OBJECT.sign may be set.
“+” indicates that both may be set but one shall be set. “|” indicates the optional settings.

From this table, the following unmarshaling code is generated.
1
2

TPM_RCTPMU_PUBLIC_PARMS_Unmarshal(TPMU_PUBLIC_PARMS *target, BYTE **buffer, INT32 *size,
UINT32 selector)

3

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

{
switch(selector) {#if ALG_KEYEDHASH
case TPM_ALG_KEYEDHASH:
return TPMS_KEYEDHASH_PARMS_Unmarshal((TPMS_KEYEDHASH_PARMS *)&(target->keyedHash), buffer, size);#endif#if ALG_SYMCIPHER
case TPM_ALG_SYMCIPHER:
return TPMT_SYM_DEF_OBJECT_Unmarshal((TPMT_SYM_DEF_OBJECT *)&(target->symDetail), buffer, size, FALSE);#endif#if ALG_RSA
case TPM_ALG_RSA:
return TPMS_RSA_PARMS_Unmarshal((TPMS_RSA_PARMS *)&(target->rsaDetail), buffer, size);#endif#if ALG_ECC
case TPM_ALG_ECC:
return TPMS_ECC_PARMS_Unmarshal((TPMS_ECC_PARMS *)&(target->eccDetail), buffer, size);#endif}Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 57
December 24, 2020

Part 4: Supporting Routines

27
28

Trusted Platform Module Library

return TPM_RC_SELECTOR;}NOTE

The #if/#endif directives are added whenever a value is dependent on an algorithm ID so that removing
the algorithm definition will remove the related code.

The marshaling code for the union is:
1
2
3
4
5
6
7
8
9
10

11
12
13
14
15

16
17
18
19
20

21
22
23
24
25

26
27
28
29

UINT16
TPMU_PUBLIC_PARMS_Marshal(TPMU_PUBLIC_PARMS *source, BYTE **buffer, INT32 *size,
UINT32 selector){
switch(selector) {#if ALG_KEYEDHASH
case TPM_ALG_KEYEDHASH:
return TPMS_KEYEDHASH_PARMS_Marshal((TPMS_KEYEDHASH_PARMS *)&(source->keyedHash), buffer, size);#endif#if ALG_SYMCIPHER
case TPM_ALG_SYMCIPHER:
return TPMT_SYM_DEF_OBJECT_Marshal((TPMT_SYM_DEF_OBJECT *)&(source->symDetail), buffer, size);#endif#if ALG_RSA
case TPM_ALG_RSA:
return TPMS_RSA_PARMS_Marshal((TPMS_RSA_PARMS *)&(source->rsaDetail), buffer, size);#endif#if ALG_ECC
case TPM_ALG_ECC:
return TPMS_ECC_PARMS_Marshal((TPMS_ECC_PARMS *)&(source->eccDetail), buffer, size);#endif}assert(1);return 0;}

For the marshaling and unmarshaling code, a value in the structure containing the union provides thevalue used for selector. The example in the next clause illustrates this.

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 58
December 24, 2020

Part 4: Supporting Routines

9.10.4

Trusted Platform Module Library

Unmarshal and Marshal a Structure

In TPM 2.0 Part 2, the TPMT_PUBLIC structure is defined by:
Table xxx — Definition of TPMT_PUBLIC Structure
Parameter

Type

Description

type

TPMI_ALG_PUBLIC“algorithm” associated with this object

nameAlg+TPMI_ALG_HASH

algorithm used for computing the Name of the object
NOTE

The "+" indicates that the instance of a TPMT_PUBLIC may havea "+" to indicate that the nameAlg may be TPM_ALG_NULL.

objectAttributes

TPMA_OBJECT

attributes that, along with type, determine the manipulations of thisobjectauthPolicy

TPM2B_DIGEST

optional policy for using this key
The policy is computed using the nameAlg of the object.
shall be the Empty Buffer if no authorization policy is present[type]parameters TPMU_PUBLIC_PARMS the algorithm or structure details[type]unique

TPMU_PUBLIC_ID

the unique identifier of the structure
For an asymmetric key, this would be the public key.

This structure is tagged (the first value indicates the structure type), and that tag is used to determine how
the parameters and unique fields are unmarshaled and marshaled. The use of the type for specifying theunion selector is emphasized below.
The unmarshaling code for the structure in the table above is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

TPM_RCTPMT_PUBLIC_Unmarshal(TPMT_PUBLIC *target, BYTE **buffer, INT32 *size, BOOL flag){
TPM_RC
result;result = TPMI_ALG_PUBLIC_Unmarshal((TPMI_ALG_PUBLIC *)&(target->type),
buffer, size);if(result != TPM_RC_SUCCESS)
return result;result = TPMI_ALG_HASH_Unmarshal((TPMI_ALG_HASH *)&(target->nameAlg),
buffer, size, flag);if(result != TPM_RC_SUCCESS)
return result;result = TPMA_OBJECT_Unmarshal((TPMA_OBJECT *)&(target->objectAttributes),
buffer, size);if(result != TPM_RC_SUCCESS)
return result;result = TPM2B_DIGEST_Unmarshal((TPM2B_DIGEST *)&(target->authPolicy),
buffer, size);if(result != TPM_RC_SUCCESS)
return result;result = TPMU_PUBLIC_PARMS_Unmarshal((TPMU_PUBLIC_PARMS *)&(target->parameters),
buffer, size,);
if(result != TPM_RC_SUCCESS)
return result;result = TPMU_PUBLIC_ID_Unmarshal((TPMU_PUBLIC_ID *)&(target->unique),buffer, size,)
if(result != TPM_RC_SUCCESS)
return result;
return TPM_RC_SUCCESS;}

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 59
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

The marshaling code for the TPMT_PUBLIC structure is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

UINT16
TPMT_PUBLIC_Marshal(TPMT_PUBLIC *source, BYTE **buffer, INT32 *size){
UINT16
result = 0;
result = (UINT16)(result + TPMI_ALG_PUBLIC_Marshal((TPMI_ALG_PUBLIC *)&(source->type), buffer, size));
result = (UINT16)(result + TPMI_ALG_HASH_Marshal((TPMI_ALG_HASH *)&(source->nameAlg), buffer, size));
result = (UINT16)(result + TPMA_OBJECT_Marshal((TPMA_OBJECT *)&(source->objectAttributes), buffer, size));
result = (UINT16)(result + TPM2B_DIGEST_Marshal((TPM2B_DIGEST *)&(source->authPolicy), buffer, size));
result = (UINT16)(result + TPMU_PUBLIC_PARMS_Marshal((TPMU_PUBLIC_PARMS *)&(source->parameters), buffer, size,));
result = (UINT16)(result + TPMU_PUBLIC_ID_Marshal((TPMU_PUBLIC_ID *)&(source->unique), buffer, size,));
return result;}9.10.5

Unmarshal and Marshal an Array

In TPM 2.0 Part 2, the TPML_DIGEST is defined by:
Table xxx — Definition of TPML_DIGEST Structure
Parameter

Type

Description

count {2:}UINT32

number of digests in the list, minimum is twodigests[count]{:8}

TPM2B_DIGEST a list of digests
For TPM2_PolicyOR(), all digests will have been
computed using the digest of the policy session. For
TPM2_PCR_Read(), each digest will be the size of thedigest for the bank containing the PCR.

#TPM_RC_SIZE

response code when count is not at least two or isgreater than 8

The digests parameter is an array of up to count structures (TPM2B_DIGESTS). The auto-generated
code to Unmarshal this structure is:
1
2
3
4
5
6
7
8
9
10
11
12
13

TPM_RC
TPML_DIGEST_Unmarshal(TPML_DIGEST *target, BYTE **buffer, INT32 *size){
TPM_RC
result;result = UINT32_Unmarshal((UINT32 *)&(target->count), buffer, size);if(result != TPM_RC_SUCCESS)
return result;if( (target->count < 2))// This check is triggered by the {2:} notation// on ‘count’

return TPM_RC_SIZE;
if((target->count) > 8)

Family “2.0”Level 00 Revision 01.64// This check is triggered by the {:8} notation

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 60
December 24, 2020

Part 4: Supporting Routines
14
15
16
17
18
19
20
21
22
23

Trusted Platform Module Library// on ‘digests’.

return TPM_RC_SIZE;
result = TPM2B_DIGEST_Array_Unmarshal((TPM2B_DIGEST *)(target->digests),
buffer, size,
if(result != TPM_RC_SUCCESS)
return result;);

return TPM_RC_SUCCESS;}

The routine unmarshals a count value and passes that value to a routine that unmarshals an array of
TPM2B_DIGEST values. The unmarshaling code for the array is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14

TPM_RCTPM2B_DIGEST_Array_Unmarshal(TPM2B_DIGEST *target, BYTE **buffer, INT32 *size,
INT32 count){
TPM_RC
result;INT32 i;for(i = 0; i < count; i++) {result = TPM2B_DIGEST_Unmarshal(&target[i], buffer, size);if(result != TPM_RC_SUCCESS)
return result;}
return TPM_RC_SUCCESS;}

Marshaling of the TPML_DIGEST uses a similar scheme with a structure specifying the number of
elements in an array and a subsequent call to a routine to marshal an array of that type.
1
2
3
4
5
6
7
8
9
10
11
12

UINT16
TPML_DIGEST_Marshal(TPML_DIGEST *source, BYTE **buffer, INT32 *size){
UINT16
result = 0;
result = (UINT16)(result + UINT32_Marshal((UINT32 *)&(source->count), buffer,size));
result = (UINT16)(result + TPM2B_DIGEST_Array_Marshal((TPM2B_DIGEST *)(source->digests), buffer, size,(INT32)(source->count)));return result;}

The marshaling code for the array is:
1
2
3
4
5
6
7
8
9
10
11
12
13

TPM_RCTPM2B_DIGEST_Array_Unmarshal(TPM2B_DIGEST *target, BYTE **buffer, INT32 *size,
INT32 count){
TPM_RC
result;INT32 i;for(i = 0; i < count; i++) {result = TPM2B_DIGEST_Unmarshal(&target[i], buffer, size);if(result != TPM_RC_SUCCESS)
return result;}
return TPM_RC_SUCCESS;}

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 61
December 24, 2020

Part 4: Supporting Routines

9.10.6

Trusted Platform Module Library

TPM2B Handling

A TPM2B structure is handled as a special case. The unmarshaling code is similar to what is shown in
9.10.5 but the unmarshaling/marshaling is to a union element. Each TPM2B is a union of two sized
buffers, one of which is type specific (the ‘t’ element) and the other is a generic value (the ‘b’ element).
This allows each of the TPM2B structures to have some inheritance property with all other TPM2B. The
purpose is to allow functions that have parameters that can be any TPM2B structure while allowing otherfunctions to be specific about the type of the TPM2B that is used. When the generic structure is allowed,
the input parameter would use the ‘b’ element and when the type-specific structure is required, the ‘t’element is used.
When marshaling a TPM2B where the second member is a BYTE array, the size parameter indicates thesize of the array. The second member can also be a structure. In this case, the caller does not prefill thesize member. The marshaling code must marshal the structure and then back fill the calculated size.
Table xxx — Definition of TPM2B_EVENT Structure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Parameter

Type

Description

size

UINT16Size of the operandbuffer [size] {:1024}

BYTE

The operand

TPM_RCTPM2B_EVENT_Unmarshal(TPM2B_EVENT *target, BYTE **buffer, INT32 *size){
TPM_RC
result;result = UINT16_Unmarshal((UINT16 *)&(target->t.size), buffer, size);if(result != TPM_RC_SUCCESS)
return result;// if size equal to 0, the rest of the structure is a zero buffer// so stop processing
if(target->t.size == 0)
return TPM_RC_SUCCESS;
if((target->t.size) > 1024)// This check is triggered by the {:1024}// notation on ‘buffer’
return TPM_RC_SIZE;
result = BYTE_Array_Unmarshal((BYTE *)(target->t.buffer), buffer, size,(INT32)(target->t.size));if(result != TPM_RC_SUCCESS)
return result;
return TPM_RC_SUCCESS;}

using these structure definitions:
1
2
3
4
5
6
7

typedef union {struct {UINT16BYTE}
TPM2B} TPM2B_EVENT;

9.10.79.10.7.1

size;
buffer[1024];t;b;

Table Marshal Headers
TableMarshal.h[[TableMarshal_h]]
Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 62
December 24, 2020

Part 4: Supporting Routines

9.10.7.2

Trusted Platform Module Library

TableMarshalDefines.h[[TableMarshalDefines_h]]9.10.7.3

TableMarshalTypes.h[[TableMarshalTypes_h]]9.10.89.10.8.1

Table Marshal Source
TableDrivenMarshal.c[[TableDrivenMarshal]]
9.10.8.2

TableMarshalData.c[[TableMarshalData]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 63
December 24, 2020

Part 4: Supporting Routines

9.11

Trusted Platform Module Library

MathOnByteBuffers.c[[MathOnByteBuffers]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 64
December 24, 2020

Part 4: Supporting Routines

9.12

Trusted Platform Module Library

Memory.c[[Memory]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 65
December 24, 2020

Part 4: Supporting Routines

9.13

Trusted Platform Module LibraryPower.c[[Power]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 66
December 24, 2020

Part 4: Supporting Routines

9.14

Trusted Platform Module Library

PropertyCap.c[[PropertyCap]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 67
December 24, 2020

Part 4: Supporting Routines

9.15

Trusted Platform Module Library

Response.c[[Response]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 68
December 24, 2020

Part 4: Supporting Routines

9.16

Trusted Platform Module Library

ResponseCodeProcessing.c[[ResponseCodeProcessing]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 69
December 24, 2020

Part 4: Supporting Routines

9.17

Trusted Platform Module Library

TpmFail.c[[TpmFail]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 70
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10 Cryptographic Functions
10.1
10.1.1Headers
BnValues.h[[BnValues_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 71
December 24, 2020

Part 4: Supporting Routines

10.1.2Trusted Platform Module Library

CryptEcc.h[[CryptEcc_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 72
December 24, 2020

Part 4: Supporting Routines

10.1.3

Trusted Platform Module Library

CryptHash.h[[CryptHash_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 73
December 24, 2020

Part 4: Supporting Routines

10.1.4

Trusted Platform Module Library

CryptRand.h[[CryptRand_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 74
December 24, 2020

Part 4: Supporting Routines

10.1.5

Trusted Platform Module Library

CryptRsa.h[[CryptRsa_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 75
December 24, 2020

Part 4: Supporting Routines

10.1.6

Trusted Platform Module Library

CryptTest.h[[CryptTest_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 76
December 24, 2020

Part 4: Supporting Routines

10.1.7

Trusted Platform Module Library

HashTestData.h[[HashTestData_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 77
December 24, 2020

Part 4: Supporting Routines

10.1.8Trusted Platform Module Library

KdfTestData.h[[KdfTestData_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 78
December 24, 2020

Part 4: Supporting Routines

10.1.9Trusted Platform Module Library

RsaTestData.h[[RsaTestData_h]]10.1.10 SelfTest.h[[SelfTest_h]]10.1.11 SupportLibraryFunctionPrototypes_fp.h[[SupportLibraryFunctionPrototypes_fp_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 79
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.1.12 SymmetricTestData.h[[SymmetricTestData_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 80
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.1.13 SymmetricTest.h[[SymmetricTest_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 81
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.1.14 EccTestData.h[[EccTestData_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 82
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.1.15 CryptSym.h[[CryptSym_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 83
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.1.16 OIDs.h[[OIDs_h]]10.1.17 PRNG_TestVectors.h[[PRNG_TestVectors_h]]10.1.18 TpmAsn1.h[[TpmAsn1_h]]
10.1.19 X509.h[[X509_h]]
10.1.20 TpmAlgorithmDefines.h
This file contains the algorithm values from the TCG Algorithm Registry.
[[TpmAlgorithmDefines_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 84
December 24, 2020

Part 4: Supporting Routines

10.2
10.2.1

Trusted Platform Module Library

SourceAlgorithmTests.c[[AlgorithmTests]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 85
December 24, 2020

Part 4: Supporting Routines

10.2.2Trusted Platform Module Library

BnConvert.c[[BnConvert]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 86
December 24, 2020

Part 4: Supporting Routines

10.2.3

Trusted Platform Module Library

BnMath.c[[BnMath]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 87
December 24, 2020

Part 4: Supporting Routines

10.2.4

Trusted Platform Module Library

BnMemory.c[[BnMemory]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 88
December 24, 2020

Part 4: Supporting Routines

10.2.5

Trusted Platform Module Library

CryptCmac.c[[CryptCmac]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 89
December 24, 2020

Part 4: Supporting Routines

10.2.6

Trusted Platform Module Library

CryptUtil.c[[CryptUtil]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 90
December 24, 2020

Part 4: Supporting Routines

10.2.7

Trusted Platform Module Library

CryptSelfTest.c[[CryptSelfTest]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 91
December 24, 2020

Part 4: Supporting Routines

10.2.8Trusted Platform Module Library

CryptEccData.c[[CryptEccData]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 92
December 24, 2020

Part 4: Supporting Routines

10.2.9Trusted Platform Module Library

CryptDes.c[[CryptDes]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 93
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.10 CryptEccKeyExchange.c[[CryptEccKeyExchange]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 94
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.11 CryptEccMain.c[[CryptEccMain]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 95
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.12 CryptEccSignature.c[[CryptEccSignature]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 96
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.13 CryptHash.c[[CryptHash]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 97
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.14 CryptPrime.c[[CryptPrime]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 98
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.15 CryptPrimeSieve.c[[CryptPrimeSieve]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 99
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.16 CryptRand.c[[CryptRand]]Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 100
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.17 CryptRsa.c[[CryptRsa]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 101
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.18 CryptSmac.c[[CryptSmac]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 102
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.19 CryptSym.c[[CryptSym]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 103
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.20 PrimeData.c[[PrimeData]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 104
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.21 RsaKeyCache.c[[RsaKeyCache]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 105
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.22 Ticket.c[[Ticket]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 106
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.23 TpmAsn1.c[[TpmAsn1]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 107
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.24 X509_ECC.c[[X509_ECC]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 108
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.25 X509_RSA.c[[X509_RSA]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 109
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.26 X509_spt.c[[X509_spt]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 110
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

10.2.27 AC_spt.c[[AC_spt]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 111
December 24, 2020

Part 4: Supporting Routines

10.2.28

Trusted Platform Module Library

CryptEccCrypt.c[[CryptEccCrypt]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 112
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

Annex A
(informative)
Implementation Dependent
A.1

Introduction

This header file contains definitions that are used to define a TPM profile. The values are chosen by the
manufacturer. The values here are chosen to represent a full featured TPM so that all of the TPM’s
capabilities can be simulated and tested. This file would change based on the implementation.
The file listed below was generated by an automated tool using three documents as inputs. They are:
The TCG Algorithm Registry,
Part 2 of this specification, andA purpose-built document that contains vendor-specific information in tables.
All of the values in this file have #ifdef ‘guards’ so that they may be defined in a command line.
Additionally, TpmBuildSwitches.h allows an additional file to be specified in the compiler command line
and preset any of these values.
A.2

TpmProfile.h[[TpmProfile_h]]
A.3

TpmSizeChecks.c[[TpmSizeChecks]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 113
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

Annex B(informative)
Library-SpecificB.1

Introduction

This clause contains the files that are specific to a cryptographic library used by the TPM code.
Three categories are defined for cryptographic functions:
big number math (asymmetric cryptography),symmetric ciphers, andhash functions.
The code is structured to make it possible to use different libraries for different categories. For example,
one might choose to use OpenSSL for its math library, but use a different library for hashing andsymmetric cryptography. Since OpenSSL supports all three categories, it might be more typical to
combine libraries of specific functions; that is, one library might only contain block ciphers while anothersupports big number math.

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 114
December 24, 2020

Part 4: Supporting Routines

B.2

Trusted Platform Module Library

OpenSSL-Specific Files

B.2.1.

Introduction

The following files are specific to a port that uses the OpenSSL library for cryptographic functions.
B.2.2.
B.2.2.1.

Header Files
TpmToOsslHash.h[[TpmToOsslHash_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 115
December 24, 2020

Part 4: Supporting Routines

B.2.2.2.

Trusted Platform Module Library

TpmToOsslMath.h[[TpmToOsslMath_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 116
December 24, 2020

Part 4: Supporting Routines

B.2.2.3.

Trusted Platform Module Library

TpmToOsslSym.h[[TpmToOsslSym_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 117
December 24, 2020

Part 4: Supporting Routines

B.2.3.
B.2.3.1.

Trusted Platform Module Library

Source Files
TpmToOsslDesSupport.c[[TpmToOsslDesSupport]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 118
December 24, 2020

Part 4: Supporting Routines

B.2.3.2.

Trusted Platform Module Library

TpmToOsslMath.c[[TpmToOsslMath]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 119
December 24, 2020

Part 4: Supporting Routines

B.2.3.3.

Trusted Platform Module Library

TpmToOsslSupport.c[[TpmToOsslSupport]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 120
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

Annex C
(informative)
Simulation Environment
C.1

Introduction

These files are used to simulate some of the implementation-dependent hardware of a TPM. These files
are provided to allow creation of a simulation environment for the TPM. These files are not expected to bepart of a hardware TPM implementation.
C.2

Cancel.c[[Cancel]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 121
December 24, 2020

Part 4: Supporting Routines

C.3

Trusted Platform Module Library

Clock.c[[Clock]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 122
December 24, 2020

Part 4: Supporting Routines

C.4

Trusted Platform Module Library

Entropy.c[[Entropy]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 123
December 24, 2020

Part 4: Supporting Routines

C.5

Trusted Platform Module Library

LocalityPlat.c[[LocalityPlat]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 124
December 24, 2020

Part 4: Supporting Routines

C.6

Trusted Platform Module Library

NVMem.c[[NVMem]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 125
December 24, 2020

Part 4: Supporting Routines

C.7

Trusted Platform Module LibraryPowerPlat.c[[PowerPlat]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 126
December 24, 2020

Part 4: Supporting Routines

C.8

Trusted Platform Module Library

PlatformData.h[[PlatformData_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 127
December 24, 2020

Part 4: Supporting Routines

C.9

Trusted Platform Module Library

PlatformData.c[[PlatformData]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 128
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

C.10 PPPlat.c[[PPPlat]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 129
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

C.11 RunCommand.c[[RunCommand]]Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 130
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

C.12 Unique.c[[Unique]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 131
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

C.13 DebugHelpers.c[[DebugHelpers]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 132
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

C.14 Platform.h[[Platform_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 133
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

C.15 PlatformACT.h[[PlatformACT_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 134
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

C.16 PlatformACT.c[[PlatformACT]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 135
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

C.17 PlatformClock.h[[PlatformClock_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 136
December 24, 2020

Part 4: Supporting Routines

Trusted Platform Module Library

Annex D
(informative)
Remote Procedure Interface
D.1

Introduction

These files provide an RPC interface for a TPM simulation.
The simulation uses two ports: a command port and a hardware simulation port. Only TPM commands
defined in TPM 2.0 Part 3 are sent to the TPM on the command port. The hardware simulation port isused to simulate hardware events such as power on/off and locality; and indications such as_TPM_HashStart.

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 137
December 24, 2020

Part 4: Supporting Routines

D.2

Trusted Platform Module Library

TpmTcpProtocol.h[[TpmTcpProtocol_h]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 138
December 24, 2020

Part 4: Supporting Routines

D.3

Trusted Platform Module Library

TcpServer.c[[TcpServer]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 139
December 24, 2020

Part 4: Supporting Routines

D.4

Trusted Platform Module Library

TPMCmdp.c[[TPMCmdp]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 140
December 24, 2020

Part 4: Supporting Routines

D.5

Trusted Platform Module Library

TPMCmds.c[[TPMCmds]]

Family “2.0”Level 00 Revision 01.64

TCG PUBLIC REVIEW
Copyright © TCG 2006-2021

Page 141
December 24, 2020