-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtpm_funcs.c
More file actions
415 lines (349 loc) · 11.7 KB
/
tpm_funcs.c
File metadata and controls
415 lines (349 loc) · 11.7 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// SPDX-License-Identifier: MIT
/*
MIT License
Copyright (c) 2025 UBITECH Ltd (https://ubitech.eu)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "tpm_funcs.h"
#include <ibmtss/TPM_Types.h>
#include <ibmtss/Unmarshal_fp.h>
#include <ibmtss/tss.h>
#include <ibmtss/tsscrypto.h>
#include <ibmtss/tssmarshal.h>
#include <ibmtss/tssresponsecode.h>
#include <ibmtss/tsstransmit.h>
#include <ibmtss/tssutils.h>
#include <string.h>
/**
* Translate a TPM response code into a human-readable message and optionally exit.
*
* Parameters:
* - last_err: TPM_RC error code.
* - isCritical: If non-zero, the function terminates the process.
*/
void handle_TPM_error(TPM_RC last_err, int isCritical);
void handle_TPM_error(TPM_RC last_err, int isCritical) {
const char* msg;
const char* submsg;
const char* num;
TSS_ResponseCode_toString(&msg, &submsg, &num, last_err);
printf("An error has occurred: %s%s%s\n", msg, submsg, num);
if (isCritical) {
exit(EXIT_FAILURE);
}
}
/**
* Initialize a TSS/TPM context and execute TPM2_Startup.
*
* Parameters:
* - ctx: Optional pre-existing context (ignored, created internally).
* - locality: Interface string (e.g., "/dev/tpmrm0" or hostname:port).
*
* Returns:
* - Pointer to initialized TSS_CONTEXT, or NULL on failure.
*/
TSS_CONTEXT* start_tpm(TSS_CONTEXT* ctx, const char* locality) {
TPM_RC rc = 0;
Startup_In in;
TPM_SU startupType = TPM_SU_CLEAR;
if (rc == 0) {
rc = TSS_Create(&ctx);
if (rc != 0) {
handle_TPM_error(rc, 1);
return NULL;
}
}
if ((rc == 0) && (locality != NULL)) {
TSS_SetProperty(ctx, TPM_COMMAND_PORT, locality);
} else if (rc == 0) {
TSS_SetProperty(ctx, TPM_INTERFACE_TYPE, "dev");
}
if (rc == 0) {
in.startupType = startupType;
rc = TSS_Execute(ctx, NULL, (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_Startup, TPM_RH_NULL,
NULL, 0);
if (rc != 0) {
handle_TPM_error(rc, 1);
}
}
return ctx;
}
/**
* Execute TPM2_Shutdown and free the TSS context.
* Safe to call with NULL.
*/
void shutdown_tpm(TSS_CONTEXT* ctx) {
TPM_RC rc = 0;
Shutdown_In in;
if (ctx == NULL) {
return;
}
in.shutdownType = TPM_SU_CLEAR;
rc = TSS_Execute(ctx, NULL, (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_Shutdown, TPM_RH_NULL, NULL,
0);
if (rc != 0) {
handle_TPM_error(rc, 1);
}
TSS_Delete(ctx);
}
/**
* Wrapper for TPM2_CreatePrimary.
*
* Parameters:
* - ctx: TSS context.
* - public_template: Public properties of the key to create.
* - Hierarchy: Parent hierarchy (e.g., TPM_RH_OWNER).
*
* Returns:
* - CreatePrimary_Out with handle and public data (fatal on TPM error).
*/
CreatePrimary_Out create_primary(TSS_CONTEXT* ctx, TPM2B_PUBLIC public_template,
TPMI_RH_HIERARCHY Hierarchy) {
CreatePrimary_In in_create;
CreatePrimary_Out out_create;
TPMI_RH_HIERARCHY primaryHandle = Hierarchy;
const char* parentPasswordPtr = NULL;
TPMI_SH_AUTH_SESSION sessionHandle0 = TPM_RS_PW;
unsigned int sessionAttributes0 = 0;
TPMI_SH_AUTH_SESSION sessionHandle1 = TPM_RH_NULL;
unsigned int sessionAttributes1 = 0;
TPMI_SH_AUTH_SESSION sessionHandle2 = TPM_RH_NULL;
unsigned int sessionAttributes2 = 0;
TPM_RC rc = 0;
in_create.primaryHandle = primaryHandle;
in_create.inSensitive.sensitive.userAuth.t.size = 0;
in_create.inSensitive.sensitive.data.t.size = 0;
in_create.inPublic = public_template;
in_create.outsideInfo.t.size = 0;
in_create.creationPCR.count = 0;
rc = TSS_Execute(ctx, (RESPONSE_PARAMETERS*)&out_create, (COMMAND_PARAMETERS*)&in_create, NULL,
TPM_CC_CreatePrimary, sessionHandle0, parentPasswordPtr, sessionAttributes0,
sessionHandle1, NULL, sessionAttributes1, sessionHandle2, NULL,
sessionAttributes2, TPM_RH_NULL, NULL, 0);
if (rc != TPM_RC_SUCCESS) {
handle_TPM_error(rc, 1);
}
return out_create;
}
/**
* Wrapper for TPM2_Commit used in ECDAA operations.
*
* Parameters:
* - ctx: TSS context.
* - key: Signing key handle.
* - point: Optional point P1; if NULL, empty P1 is used.
* - session: Authorized policy session handle.
* - secret: Optional s2 parameter.
* - param: Optional y2 parameter.
*
* Returns:
* - Commit_Out structure (fatal on TPM error).
*/
Commit_Out commit(TSS_CONTEXT* ctx, TPM_HANDLE key, TPM2B_ECC_POINT* point,
TPMI_SH_AUTH_SESSION session, TPM2B_SENSITIVE_DATA* secret,
TPM2B_ECC_PARAMETER* param) {
Commit_In in;
Commit_Out out;
TPM_RC rc = 0;
in.signHandle = key;
if (point != NULL) {
in.P1 = *point;
} else {
in.P1.size = 0;
in.P1.point.x.t.size = 0;
in.P1.point.y.t.size = 0;
}
if (secret != NULL) {
in.s2 = *secret;
} else {
in.s2.t.size = 0;
}
if (param != NULL) {
in.y2 = *param;
} else {
in.y2.t.size = 0;
}
rc = TSS_Execute(ctx, (RESPONSE_PARAMETERS*)&out, (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_Commit,
session, NULL, 0, TPM_RH_NULL, NULL, 0);
if (rc != TPM_RC_SUCCESS) {
handle_TPM_error(rc, 1);
return out;
}
return out;
}
/**
* Wrapper for TPM2_Hash.
*
* Parameters:
* - ctx: TSS context.
* - buffer: Input buffer to hash.
* - hierarchy: Authorization hierarchy for hash ticket.
* - hashAlg: Hash algorithm (e.g., TPM_ALG_SHA256).
*
* Returns:
* - Hash_Out with digest and validation ticket.
*/
Hash_Out hash(TSS_CONTEXT* ctx, TPM2B_MAX_BUFFER* buffer, TPM_HANDLE hierarchy,
TPMI_ALG_HASH hashAlg) {
Hash_In in;
Hash_Out out;
TPM_RC rc = 0;
in.data = *buffer;
in.hashAlg = hashAlg;
in.hierarchy = hierarchy;
rc = TSS_Execute(ctx, (RESPONSE_PARAMETERS*)&out, (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_Hash,
TPM_RH_NULL, NULL, 0);
if (rc != TPM_RC_SUCCESS) {
handle_TPM_error(rc, 1);
return out;
}
return out;
}
/**
* Wrapper for TPM2_Sign using an active policy session.
*
* Parameters:
* - ctx: TSS context.
* - key: Signing key handle.
* - session: Policy session authorizing TPM2_Sign.
* - scheme: Signing scheme (e.g., ECDAA parameters).
* - hash: Precomputed digest and validation.
*
* Returns:
* - Sign_Out with signature.
*/
Sign_Out sign(TSS_CONTEXT* ctx, TPM_HANDLE key, StartAuthSession_Out session,
TPMT_SIG_SCHEME* scheme, Hash_Out hash) {
Sign_In in;
Sign_Out out;
TPM_RC rc = 0;
TPMI_SH_AUTH_SESSION sessionHandle0 = session.sessionHandle;
unsigned int sessionAttributes0 = 1;
unsigned int sessionAttributes1 = 0;
unsigned int sessionAttributes2 = 0;
in.keyHandle = key;
in.inScheme = *scheme;
in.digest = hash.outHash;
in.validation = hash.validation;
rc =
TSS_Execute(ctx, (RESPONSE_PARAMETERS*)&out, (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_Sign,
sessionHandle0, NULL, sessionAttributes0, TPM_RH_NULL, NULL, sessionAttributes1,
TPM_RH_NULL, NULL, sessionAttributes2, TPM_RH_NULL, NULL, 0);
if (rc != TPM_RC_SUCCESS) {
handle_TPM_error(rc, 1);
return out;
}
return out;
}
/**
* Start a TPM authorization session (HMAC or policy).
*
* Parameters:
* - ctx: TSS context.
* - session_type: TPM_SE_POLICY or TPM_SE_HMAC.
*
* Returns:
* - StartAuthSession_Out with session handle.
*/
StartAuthSession_Out auth_session(TSS_CONTEXT* ctx, TPMI_SH_POLICY session_type) {
StartAuthSession_In in;
StartAuthSession_Out out;
TPM_RC rc = 0;
in.tpmKey = TPM_RH_NULL;
in.bind = TPM_RH_NULL;
in.sessionType = session_type;
in.nonceCaller.t.size = 0;
in.symmetric.algorithm = TPM_ALG_NULL;
in.authHash = TPM_ALG_SHA256;
rc = TSS_Execute(ctx, (RESPONSE_PARAMETERS*)&out, (COMMAND_PARAMETERS*)&in, NULL,
TPM_CC_StartAuthSession, TPM_RH_NULL, NULL, 0);
if (rc != TPM_RC_SUCCESS) {
handle_TPM_error(rc, 1);
return out;
}
return out;
}
/**
* Flush a TPM handle from context.
*/
void flush_context(TSS_CONTEXT* ctx, TPM_HANDLE handle) {
TPM_RC rc;
FlushContext_In in;
in.flushHandle = handle;
rc = TSS_Execute(ctx, NULL, (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_FlushContext, TPM_RH_NULL,
NULL, 0);
if (rc != TPM_RC_SUCCESS) {
handle_TPM_error(rc, 1);
}
}
/**
* Apply PolicyCommandCode to a session to restrict it to a TPM command.
*
* Parameters:
* - ctx: TSS context.
* - session: Auth session.
* - commandCode: TPM_CC to authorize.
*
* Returns:
* - TPM_RC result code (fatal logged on failure).
*/
TPM_RC policy_command_code(TSS_CONTEXT* ctx, StartAuthSession_Out session, TPM_CC commandCode) {
PolicyCommandCode_In in;
TPM_RC rc = 0;
in.policySession = session.sessionHandle;
in.code = commandCode;
rc = TSS_Execute(ctx, NULL, (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_PolicyCommandCode,
TPM_RH_NULL, NULL, 0);
if (rc != TPM_RC_SUCCESS) {
handle_TPM_error(rc, 1);
return rc;
}
return rc;
}
/**
* Apply PolicyOR with a list of digests to the session policy.
*
* Parameters:
* - ctx: TSS context.
* - session: Auth session.
* - digestArray: Array of digests to OR.
* - len: Number of digests.
*
* Returns:
* - TPM_RC result code (fatal logged on failure).
*/
TPM_RC policy_or(TSS_CONTEXT* ctx, StartAuthSession_Out session, TPM2B_DIGEST* digestArray,
size_t len) {
TPM_RC rc = 0;
PolicyOR_In in;
TPMI_SH_AUTH_SESSION sessionHandle0 = TPM_RH_NULL;
unsigned int sessionAttributes0 = 0;
TPMI_SH_AUTH_SESSION sessionHandle1 = TPM_RH_NULL;
unsigned int sessionAttributes1 = 0;
TPMI_SH_AUTH_SESSION sessionHandle2 = TPM_RH_NULL;
unsigned int sessionAttributes2 = 0;
in.policySession = session.sessionHandle;
in.pHashList.count = (UINT32)len;
for (int i = 0; i < len; i++)
in.pHashList.digests[i] = digestArray[i];
rc = TSS_Execute(ctx, NULL, (COMMAND_PARAMETERS*)&in, NULL, TPM_CC_PolicyOR, sessionHandle0,
NULL, sessionAttributes0, sessionHandle1, NULL, sessionAttributes1,
sessionHandle2, NULL, sessionAttributes2, TPM_RH_NULL, NULL, 0);
if (rc != TPM_RC_SUCCESS)
handle_TPM_error(rc, 1);
return rc;
}