-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtpmspec.hpp
314 lines (275 loc) · 6.31 KB
/
tpmspec.hpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*++
Copyright (c) Alex Ionescu. All rights reserved.
Module Name:
tpmspec.hpp
Abstract:
This header contains a subset of the TPM2.0 Specification relevant to the
utility, using modern C++ features instead of the C89 style of the usual
auto-generated files that the Microsoft tools provide. Naming conventions
are kept to allow easy referencing of the specification.
Author:
Alex Ionescu (@aionescu) 11-Jun-2020 - Initial version
Environment:
Portable to any environment.
--*/
#pragma once
#pragma warning(disable:4201)
#pragma pack(push)
#pragma pack(1)
//
// TPM2.0 Session Tags
//
typedef enum _TPM_ST : uint16_t
{
TPM_ST_NO_SESSIONS = 0x8001,
TPM_ST_SESSIONS = 0x8002
} TPM_ST, TPMI_ST_COMMAND_TAG;
//
// TPM2.0 Command Codes
//
typedef enum _TPM_CC : uint32_t
{
TPM_CC_NV_UndefineSpace = 0x122,
TPM_CC_NV_WriteLock = 0x138,
TPM_CC_NV_DefineSpace = 0x12A,
TPM_CC_NV_Write = 0x137,
TPM_CC_NV_Read = 0x14E,
TPM_CC_NV_ReadLock = 0x14F,
TPM_CC_NV_ReadPublic = 0x169,
TPM_CC_GetCapability = 0x17A,
TPM_CC_GetRandom = 0x17B,
TPM_CC_Hash = 0x17D,
TPM_CC_ReadClock = 0x181
} TPM_CC;
//
// TPM2.0 Response Codes
//
typedef enum _TPM_RC : uint32_t
{
TPM_RC_SUCCESS = 0,
TPM_RC_FAILURE = 0x101,
TPM_RC_NV_RANGE = 0x146,
TPM_RC_NV_LOCKED = 0x148,
TPM_RC_NV_AUTHORIZATION = 0x149,
TPM_RC_NV_UNINITIALIZED = 0x14A,
TPM_RC_NV_DEFINED = 0x14C,
TPM_RC_HANDLE_1 = 0x18B,
} TPM_RC;
//
// TPM2.0 Handle Types
//
typedef enum _TPM_HT : uint8_t
{
TPM_HR_PCR,
TPM_HT_NV_INDEX,
TPM_HT_HMAC_SESSION,
TPM_HT_LOADED_SESSION = TPM_HT_HMAC_SESSION,
TPM_HT_POLICY_SESSION,
TPM_HT_SAVED_SESSION = TPM_HT_POLICY_SESSION,
TPM_HT_PERMANENT = 0x40,
TPM_HT_TRANSIENT = 0x80,
TPM_HT_PERSISTENT = 0x81
} TPM_HT;
#define HR_SHIFT 24
#define HR_NV_INDEX (TPM_HT_NV_INDEX << HR_SHIFT)
//
// TPM2.0 Capabilities
//
typedef enum _TPM_CAP : uint32_t
{
TPM_CAP_FIRST = 0,
TPM_CAP_ALGS = TPM_CAP_FIRST,
TPM_CAP_HANDLES
} TPM_CAP;
#define MAX_CAP_BUFFER 1024
#define MAX_CAP_DATA (MAX_CAP_BUFFER - sizeof(TPM_CAP) - sizeof(uint32_t))
#define MAX_CAP_HANDLES (MAX_CAP_DATA / sizeof(TPM_HANDLE))
//
// TPM Algorithm IDs
//
typedef enum _TPM_ALG_ID : uint16_t
{
TPM_ALG_SHA256 = 0x000B
} TPM_ALG_ID, TPMI_ALG_HASH;
//
// TPM Attributes for Non Volatile Index Values
//
typedef enum _TPMA_NV : uint32_t
{
//
// Write Permissions
//
TPMA_NV_PPWRITE = 0x00000001,
TPMA_NV_OWNERWRITE = 0x00000002,
TPMA_NV_AUTHWRITE = 0x00000004,
TPMA_NV_POLICYWRITE = 0x00000008,
//
// Types
//
TPMA_NV_COUNTER = 0x00000010,
TPMA_NV_BITS = 0x00000020,
TPMA_NV_EXTEND = 0x00000040,
TPMA_NV_RESERVED_TYPE_1 = 0x00000080,
TPMA_NV_RESERVED_TYPE_2 = 0x00000100,
TPMA_NV_RESERVED_TYPE_3 = 0x00000200,
//
// Modify Flags
//
TPMA_NV_POLICY_DELETE = 0x00000400,
TPMA_NV_WRITELOCKED = 0x00000800,
TPMA_NV_WRITEALL = 0x00001000,
TPMA_NV_WRITEDEFINE = 0x00002000,
TPMA_NV_WRITE_STCLEAR = 0x00004000,
TPMA_NV_GLOBALLOCK = 0x00008000,
//
// Read Permissions
//
TPMA_NV_PPREAD = 0x00010000,
TPMA_NV_OWNERREAD = 0x00020000,
TPMA_NV_AUTHREAD = 0x00040000,
TPMA_NV_POLICYREAD = 0x00080000,
//
// Additional Flags
//
TPMA_NV_RESERVED_FLAG_1 = 0x00100000,
TPMA_NV_RESERVED_FLAG_2 = 0x00200000,
TPMA_NV_RESERVED_FLAG_3 = 0x00400000,
TPMA_NV_RESERVED_FLAG_4 = 0x00800000,
TPMA_NV_RESERVED_FLAG_5 = 0x01000000,
TPMA_NV_NO_DA = 0x02000000,
TPMA_NV_ORDERLY = 0x04000000,
TPMA_NV_CLEAR_STCLEAR = 0x08000000,
TPMA_NV_READLOCKED = 0x10000000,
TPMA_NV_WRITTEN = 0x20000000,
TPMA_NV_PLATFORMCREATE = 0x40000000,
TPMA_NV_READ_STCLEAR = 0x80000000
} TPMA_NV;
//
// TPM2.0 Property Types
//
typedef enum _TPM_PT : uint32_t
{
TPM_PT_NONE = 0x0,
PT_FIXED = 0x100
} TPM_PT;
//
// TPM2.0 Session Attributes
//
typedef union
{
struct
{
uint8_t ContinueSession : 1;
uint8_t AuditExclusive : 1;
uint8_t AuditReset : 1;
uint8_t : 2;
uint8_t Decrypt : 1;
uint8_t Encrypt : 1;
uint8_t Audit : 1;
};
uint8_t Value;
} TPMA_SESSION;
//
// TPM2.0 Yes/No Boolean
//
typedef uint8_t TPMI_YES_NO;
//
// TPM2.0 Hash Algorithm Sizes
// Only SHA-256 supported for now
//
typedef union
{
uint8_t Sha256[32];
} TPMU_HA;
//
// Definition of a TPM2.0 Hash Agile Structure (Algorithm and Digest)
//
typedef struct
{
TPMI_ALG_HASH HashAlg;
TPMU_HA Digest;
} TPMT_HA;
//
// Definition of a Hash Digest Buffer, which encodes a TPM2.0 Hash Agile
//
typedef struct
{
uint16_t BufferSize;
TPMT_HA Buffer;
} TPM2B_DIGEST;
//
// Definition of a maximum sized buffer
//
typedef struct
{
uint16_t Size;
uint8_t Buffer[1024];
} TPM2B_MAX_BUFFER;
//
// Definition of a TPM2.0 Handle
//
typedef union
{
struct
{
uint8_t Index[3];
TPM_HT Type;
};
uint32_t Value;
} TPM_HANDLE, TPMI_RH_NV_INDEX, TPMI_RH_PROVISION, TPMI_RH_HIERARCHY,
TPMI_SH_AUTH_SESSION, TPMI_RH_NV_AUTH, TPM_RH, TPM_NV_INDEX;
static_assert(sizeof(TPM_HANDLE) == sizeof(uint32_t));
//
// Architecturally Defined Permanent Handles
//
static constexpr TPM_RH TPM_RH_OWNER = { 1, 0, 0, TPM_HT_PERMANENT };
static constexpr TPM_RH TPM_RH_NULL = { 7, 0, 0, TPM_HT_PERMANENT };
static constexpr TPM_RH TPM_RS_PW = { 9, 0, 0, TPM_HT_PERMANENT };
//
// TPM2.0 Handle List
//
typedef struct
{
uint32_t Count;
TPM_HANDLE Handle[MAX_CAP_HANDLES];
} TPML_HANDLE, *PTPML_HANDLE;
//
// TPM2.0 Ticket for Hash Check
//
typedef struct
{
TPM_ST Tag;
TPMI_RH_HIERARCHY Hierarchy;
TPM2B_DIGEST Digest;
} TPMT_TK_HASHCHECK;
//
// TPM2.0 Union of capability data returned by TPM2_CC_GetCapabilities
//
typedef union
{
TPML_HANDLE Handles;
} TPMU_CAPABILITIES, *PTPMU_CAPABILITIES;
//
// TPM2.0 Payload for each capablity returned by TPM2_CC_GetCapabilities
//
typedef struct
{
TPM_CAP Capability;
TPMU_CAPABILITIES Data;
} TPMS_CAPABILITY_DATA, *PTPMS_CAPABILITY_DATA;
//
// TPM2.0 Payload for TPM2_CC_ReadClock
//
typedef struct
{
uint64_t Clock;
uint32_t ResetCount;
uint32_t RestartCount;
TPMI_YES_NO Safe;
} TPMS_CLOCK_INFO;
typedef struct
{
uint64_t Time;
TPMS_CLOCK_INFO ClockInfo;
} TPMS_TIME_INFO;
#pragma pack(pop)