-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandCodeAttributes.c
291 lines (289 loc) · 8.49 KB
/
CommandCodeAttributes.c
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
// This file was extracted from the TCG Published
// Trusted Platform Module Library
// Part 4: Supporting Routines
// Family "2.0"
// Level 00 Revision 01.16
// October 30, 2014
#include "Tpm.h"
#include "InternalRoutines.h"
typedef UINT16 ATTRIBUTE_TYPE;
//
// The following file is produced from the command tables in part 3 of the specification. It defines the
// attributes for each of the commands.
//
// NOTE: This file is currently produced by an automated process. Files produced from Part 2 or Part 3 tables through
// automated processes are not included in the specification so that their is no ambiguity about the table
// containing the information being the normative definition.
//
#include "CommandAttributeData.c"
//
//
// Command Attribute Functions
//
// CommandAuthRole()
//
// This function returns the authorization role required of a handle.
//
// Return Value Meaning
//
// AUTH_NONE no authorization is required
// AUTH_USER user role authorization is required
// AUTH_ADMIN admin role authorization is required
// AUTH_DUP duplication role authorization is required
//
AUTH_ROLE
CommandAuthRole(
TPM_CC commandCode, // IN: command code
UINT32 handleIndex // IN: handle index (zero based)
)
{
if(handleIndex > 1)
return AUTH_NONE;
if(handleIndex == 0) {
ATTRIBUTE_TYPE properties = s_commandAttributes[commandCode - TPM_CC_FIRST];
if(properties & HANDLE_1_USER) return AUTH_USER;
if(properties & HANDLE_1_ADMIN) return AUTH_ADMIN;
if(properties & HANDLE_1_DUP) return AUTH_DUP;
return AUTH_NONE;
}
if(s_commandAttributes[commandCode - TPM_CC_FIRST] & HANDLE_2_USER)
return AUTH_USER;
return AUTH_NONE;
}
//
//
// CommandIsImplemented()
//
// This function indicates if a command is implemented.
//
// Return Value Meaning
//
// TRUE if the command is implemented
// FALSE if the command is not implemented
//
BOOL
CommandIsImplemented(
TPM_CC commandCode // IN: command code
)
{
if(commandCode < TPM_CC_FIRST || commandCode > TPM_CC_LAST)
return FALSE;
if((s_commandAttributes[commandCode - TPM_CC_FIRST] & IS_IMPLEMENTED))
return TRUE;
else
return FALSE;
}
//
//
// CommandGetAttribute()
//
// return a TPMA_CC structure for the given command code
//
TPMA_CC
CommandGetAttribute(
TPM_CC commandCode // IN: command code
)
{
UINT32 size = sizeof(s_ccAttr) / sizeof(s_ccAttr[0]);
UINT32 i;
for(i = 0; i < size; i++) {
if(s_ccAttr[i].commandIndex == (UINT16) commandCode)
return s_ccAttr[i];
}
// This function should be called in the way that the command code
// attribute is available.
FAIL(FATAL_ERROR_INTERNAL);
return s_ccAttr[0]; // Just to appease the compiler, never reached.
}
//
//
// EncryptSize()
//
// This function returns the size of the decrypt size field. This function returns 0 if encryption is not allowed
//
// Return Value Meaning
//
// 0 encryption not allowed
// 2 size field is two bytes
// 4 size field is four bytes
//
int
EncryptSize(
TPM_CC commandCode // IN: commandCode
)
{
COMMAND_ATTRIBUTES ca = s_commandAttributes[commandCode - TPM_CC_FIRST];
if(ca & ENCRYPT_2)
return 2;
if(ca & ENCRYPT_4)
return 4;
return 0;
}
//
//
// DecryptSize()
//
// This function returns the size of the decrypt size field. This function returns 0 if decryption is not allowed
//
// Return Value Meaning
//
// 0 encryption not allowed
// 2 size field is two bytes
// 4 size field is four bytes
//
int
DecryptSize(
TPM_CC commandCode // IN: commandCode
)
{
COMMAND_ATTRIBUTES ca = s_commandAttributes[commandCode - TPM_CC_FIRST];
if(ca & DECRYPT_2)
return 2;
if(ca & DECRYPT_4)
return 4;
return 0;
}
//
//
// IsSessionAllowed()
//
// This function indicates if the command is allowed to have sessions.
// This function must not be called if the command is not known to be implemented.
//
// Return Value Meaning
//
// TRUE session is allowed with this command
// FALSE session is not allowed with this command
//
BOOL
IsSessionAllowed(
TPM_CC commandCode // IN: the command to be checked
)
{
if(s_commandAttributes[commandCode - TPM_CC_FIRST] & NO_SESSIONS)
return FALSE;
else
return TRUE;
}
//
//
// IsHandleInResponse()
//
BOOL
IsHandleInResponse(
TPM_CC commandCode
)
{
if(s_commandAttributes[commandCode - TPM_CC_FIRST] & R_HANDLE)
return TRUE;
else
return FALSE;
//
}
//
//
// IsWriteOperation()
//
// Checks to see if an operation will write to NV memory
//
BOOL
IsWriteOperation(
TPM_CC command // IN: Command to check
)
{
switch (command)
{
case TPM_CC_NV_Write:
case TPM_CC_NV_Increment:
case TPM_CC_NV_SetBits:
case TPM_CC_NV_Extend:
// Nv write lock counts as a write operation for authorization purposes.
// We check to see if the NV is write locked before we do the authorization
// If it is locked, we fail the command early.
case TPM_CC_NV_WriteLock:
return TRUE;
default:
break;
}
return FALSE;
}
//
//
// IsReadOperation()
//
// Checks to see if an operation will write to NV memory
//
BOOL
IsReadOperation(
TPM_CC command // IN: Command to check
)
{
switch (command)
{
case TPM_CC_NV_Read:
case TPM_CC_PolicyNV:
case TPM_CC_NV_Certify:
// Nv read lock counts as a read operation for authorization purposes.
// We check to see if the NV is read locked before we do the authorization
// If it is locked, we fail the command early.
case TPM_CC_NV_ReadLock:
return TRUE;
default:
break;
}
return FALSE;
}
//
//
// CommandCapGetCCList()
//
// This function returns a list of implemented commands and command attributes starting from the
// command in commandCode.
//
//
//
//
// Return Value Meaning
//
// YES more command attributes are available
// NO no more command attributes are available
//
TPMI_YES_NO
CommandCapGetCCList(
TPM_CC commandCode, // IN: start command code
UINT32 count, // IN: maximum count for number of entries in
// 'commandList'
TPML_CCA *commandList // OUT: list of TPMA_CC
)
{
TPMI_YES_NO more = NO;
UINT32 i;
// initialize output handle list count
commandList->count = 0;
// The maximum count of commands that may be return is MAX_CAP_CC.
if(count > MAX_CAP_CC) count = MAX_CAP_CC;
// If the command code is smaller than TPM_CC_FIRST, start from TPM_CC_FIRST
if(commandCode < TPM_CC_FIRST) commandCode = TPM_CC_FIRST;
// Collect command attributes
for(i = commandCode; i <= TPM_CC_LAST; i++)
{
if(CommandIsImplemented(i))
{
if(commandList->count < count)
{
// If the list is not full, add the attributes for this command.
commandList->commandAttributes[commandList->count]
= CommandGetAttribute(i);
commandList->count++;
}
else
{
// If the list is full but there are more commands to report,
// indicate this and return.
more = YES;
break;
}
}
}
return more;
}