Skip to content

Commit 4ea2520

Browse files
committed
Peer review fixes.
1 parent c78d395 commit 4ea2520

4 files changed

Lines changed: 63 additions & 56 deletions

File tree

examples/gpio/gpio_config.c

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
7878
#ifdef WOLFTPM_ST33
7979
GpioConfig_In gpio;
8080
SetCommandSet_In setCmdSet;
81-
#endif
82-
#ifdef WOLFTPM_NUVOTON
81+
#elif defined(WOLFTPM_NUVOTON)
8382
CFG_STRUCT newConfig;
8483
NTC2_GetConfig_Out getConfig;
8584
NTC2_PreConfig_In preConfig;
@@ -161,7 +160,6 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
161160
printf("wolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
162161
goto exit;
163162
}
164-
printf("wolfTPM2_Init: success\n");
165163

166164
/* Get TPM capabilities, to discover the TPM vendor */
167165
rc = wolfTPM2_GetCapabilities(&dev, &caps);
@@ -172,26 +170,24 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
172170
/* Confirm the TPM vendor */
173171
#ifdef WOLFTPM_ST33
174172
if (caps.mfg != TPM_MFG_STM) {
175-
printf("TPM model mismatch. GPIO support requires an ST33 TPM 2.0 module\n");
173+
printf("TPM vendor mismatch. GPIO support requires an ST33 TPM 2.0 module\n");
176174
goto exit;
177175
}
178176

177+
/* Make sure NV Index for this GPIO is cleared before use
178+
* This way we make sure a new GPIO config can be set */
179179
#ifdef DEBUG_WOLFTPM
180180
printf("Trying to remove NV index 0x%8.8X used for GPIO\n", nvIndex);
181181
#endif
182-
183-
/* Make sure NV Index for this GPIO is cleared before use
184-
* This way we make sure a new GPIO config can be set
185-
*/
186182
rc = wolfTPM2_NVDelete(&dev, TPM_RH_OWNER, nvIndex);
187183
if (rc == TPM_RC_SUCCESS) {
188-
printf("NV index undefined\n");
184+
printf("NV Index undefined\n");
189185
}
190186
else if (rc == (TPM_RC_HANDLE | TPM_RC_2)) {
191187
printf("NV Index is available for GPIO use\n");
192188
}
193189
else {
194-
printf("wolfTPM2_NVDelete failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
190+
printf("NV Index delete failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
195191
}
196192

197193
/* GPIO un-configuration is done using NVDelete, no further action needed */
@@ -205,14 +201,14 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
205201
setCmdSet.enableFlag = 1;
206202
rc = TPM2_SetCommandSet(&setCmdSet);
207203
if (rc != TPM_RC_SUCCESS) {
208-
printf("TPM2_SetCommandSet failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
204+
printf("Enable GPIO config command failed 0x%x: %s\n",
205+
rc, TPM2_GetRCString(rc));
209206
goto exit;
210207
}
211208

212209
/* Configuring a TPM GPIO requires a PLATFORM authorization. Afterwards,
213210
* using that GPIO is up to the user. Therefore, NV Indexes are operated
214-
* using OWNER authorization. See below NVCreateAuth.
215-
*/
211+
* using OWNER authorization. See below NVCreateAuth. */
216212
XMEMSET(&gpio, 0, sizeof(gpio));
217213
gpio.authHandle = TPM_RH_PLATFORM;
218214
gpio.config.count = 1;
@@ -234,7 +230,7 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
234230
/* Prep NV attributes */
235231
parent.hndl = TPM_RH_OWNER;
236232
rc = wolfTPM2_GetNvAttributesTemplate(parent.hndl, &nvAttributes);
237-
if (rc != 0) {
233+
if (rc != TPM_RC_SUCCESS) {
238234
printf("Setting NV attributes failed\n");
239235
goto exit;
240236
}
@@ -252,15 +248,15 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
252248
BYTE dummy = 0;
253249
/* Writing a dummy byte has no impact on the input, but is required */
254250
rc = wolfTPM2_NVWriteAuth(&dev, &nv, nvIndex, &dummy, sizeof(dummy), 0);
255-
if (rc != 0) {
251+
if (rc != TPM_RC_SUCCESS) {
256252
printf("Error while configuring the GPIO as an Input.\n");
257253
}
258254
}
259255

260256
#elif defined(WOLFTPM_NUVOTON)
261257

262258
if (caps.mfg != TPM_MFG_NUVOTON) {
263-
printf("TPM model mismatch. GPIO support requires a Nuvoton NPCT7xx TPM 2.0 module\n");
259+
printf("TPM vendor mismatch. GPIO support requires Nuvoton NPCT7xx TPM 2.0 module\n");
264260
goto exit;
265261
}
266262

@@ -272,53 +268,62 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
272268
/* This procedure requires CommandCode policy and EK Auth policy */
273269
rc = wolfTPM2_StartSession(&dev, &tpmSessionIndex, NULL, NULL,
274270
TPM_SE_POLICY, TPM_ALG_NULL);
275-
if (rc == TPM_RC_SUCCESS) {
276-
printf("index ok\n");
271+
if (rc != TPM_RC_SUCCESS) {
272+
printf("wolfTPM2_StartSession index failed 0x%x: %s\n", rc,
273+
TPM2_GetRCString(rc));
274+
goto exit;
277275
}
278-
279276
rc = wolfTPM2_StartSession(&dev, &tpmSessionPlatform, NULL, NULL,
280277
TPM_SE_POLICY, TPM_ALG_NULL);
278+
if (rc != TPM_RC_SUCCESS) {
279+
printf("wolfTPM2_StartSession policy failed 0x%x: %s\n", rc,
280+
TPM2_GetRCString(rc));
281+
goto exit;
282+
}
281283

282-
if (rc == TPM_RC_SUCCESS) {
283-
#ifdef DEBUG_WOLFTPM
284-
printf("TPM2_StartAuthSession: tpmSessionIndex 0x%x\n",
285-
(word32)tpmSessionIndex.handle.hndl);
286-
printf("TPM2_StartAuthSession: tpmSessionPlatforme 0x%x\n",
287-
(word32)tpmSessionPlatform.handle.hndl);
288-
#endif
289-
290-
/* Allow object change auth */
291-
XMEMSET(&policyCC, 0, sizeof(policyCC));
292-
policyCC.policySession = tpmSessionIndex.handle.hndl;
293-
policyCC.code = TPM_CC_NV_UndefineSpaceSpecial;
294-
rc = TPM2_PolicyCommandCode(&policyCC);
295-
if (rc != TPM_RC_SUCCESS) {
296-
printf("TPM2_PolicyCommandCode failed 0x%x: %s\n", rc,
284+
#ifdef DEBUG_WOLFTPM
285+
printf("TPM2_StartAuthSession: tpmSessionIndex 0x%x\n",
286+
(word32)tpmSessionIndex.handle.hndl);
287+
printf("TPM2_StartAuthSession: tpmSessionPlatforme 0x%x\n",
288+
(word32)tpmSessionPlatform.handle.hndl);
289+
#endif
290+
291+
/* Allow object change auth */
292+
XMEMSET(&policyCC, 0, sizeof(policyCC));
293+
policyCC.policySession = tpmSessionIndex.handle.hndl;
294+
policyCC.code = TPM_CC_NV_UndefineSpaceSpecial;
295+
rc = TPM2_PolicyCommandCode(&policyCC);
296+
if (rc != TPM_RC_SUCCESS) {
297+
printf("TPM2_PolicyCommandCode failed 0x%x: %s\n", rc,
297298
TPM2_GetRCString(rc));
298-
goto exit;
299-
}
300-
printf("TPM2_PolicyCommandCode: success\n");
301-
302-
/* Provide Endorsement Auth using PolicySecret */
303-
XMEMSET(&policySecretIn, 0, sizeof(policySecretIn));
304-
policySecretIn.authHandle = TPM_RH_ENDORSEMENT;
305-
policySecretIn.policySession = tpmSessionIndex.handle.hndl;
306-
rc = TPM2_PolicySecret(&policySecretIn, &policySecretOut);
307-
if (rc == TPM_RC_SUCCESS) {
308-
printf("TPM2_PolicySecret: success\n");
309-
}
299+
goto exit;
310300
}
301+
printf("TPM2_PolicyCommandCode: success\n");
302+
303+
/* Provide Endorsement Auth using PolicySecret */
304+
XMEMSET(&policySecretIn, 0, sizeof(policySecretIn));
305+
policySecretIn.authHandle = TPM_RH_ENDORSEMENT;
306+
policySecretIn.policySession = tpmSessionIndex.handle.hndl;
307+
rc = TPM2_PolicySecret(&policySecretIn, &policySecretOut);
308+
if (rc != TPM_RC_SUCCESS) {
309+
printf("TPM2_PolicySecret failed 0x%x: %s\n", rc,
310+
TPM2_GetRCString(rc));
311+
goto exit;
312+
}
313+
printf("TPM2_PolicySecret: success\n");
311314

312315
/* Slot 0 for Index */
313316
rc = wolfTPM2_SetAuthSession(&dev, 0, &tpmSessionIndex, 0);
314317
if (rc != TPM_RC_SUCCESS) {
315-
printf("Failure to set Index auth session\n");
318+
printf("Failure to set Index auth session (0x%x: %s)\n", rc,
319+
TPM2_GetRCString(rc));
316320
goto exit;
317321
}
318322
/* Slot 1 for Platform */
319323
rc = wolfTPM2_SetAuthSession(&dev, 1, &tpmSessionPlatform, 0);
320324
if (rc != TPM_RC_SUCCESS) {
321-
printf("Failure to set Platform auth session\n");
325+
printf("Failure to set Platform auth session (0x%x: %s)\n", rc,
326+
TPM2_GetRCString(rc));
322327
goto exit;
323328
}
324329

@@ -332,6 +337,7 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
332337
printf("Deleting the NV Index failed 0x%x: %s\n", rc,
333338
TPM2_GetRCString(rc));
334339
}
340+
335341
/* Procedure for mode 4 (delete GPIO NV index) ends here */
336342
goto exit;
337343
}
@@ -396,7 +402,7 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
396402
/* Add NV attributes required by Nuvoton specification */
397403
nvAttributes |= (TPMA_NV_PLATFORMCREATE | TPMA_NV_POLICY_DELETE);
398404
nvAttributes |= (TPM_NT_ORDINARY & TPMA_NV_TPM_NT);
399-
if (rc != 0) {
405+
if (rc != TPM_RC_SUCCESS) {
400406
printf("Setting NV attributes failed\n");
401407
goto exit;
402408
}
@@ -418,7 +424,7 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
418424

419425
exit:
420426

421-
#ifdef WOLFTPM_NUVOTON
427+
#if defined(WOLFTPM_NUVOTON) && !defined(WOLFTPM_ST33)
422428
wolfTPM2_UnloadHandle(&dev, &tpmSessionIndex.handle);
423429
wolfTPM2_UnloadHandle(&dev, &tpmSessionPlatform.handle);
424430
#endif

examples/gpio/gpio_read.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ int TPM2_GPIO_Read_Example(void* userCtx, int argc, char *argv[])
9494
/* Read GPIO state */
9595
readSize = sizeof(pinState);
9696
rc = wolfTPM2_NVReadAuth(&dev, &nv, nvIndex, &pinState, &readSize, 0);
97-
if (rc != 0) {
97+
if (rc != TPM_RC_SUCCESS) {
9898
printf("Error while reading GPIO state\n");
9999
printf("Make sure GPIO has been configured with './examples/gpio/gpio_config'\n");
100100
goto exit;
@@ -112,7 +112,7 @@ int TPM2_GPIO_Read_Example(void* userCtx, int argc, char *argv[])
112112

113113
exit:
114114

115-
if (rc != 0) {
115+
if (rc != TPM_RC_SUCCESS) {
116116
printf("\nFailure 0x%x: %s\n\n", rc, wolfTPM2_GetRCString(rc));
117117
}
118118

examples/gpio/gpio_set.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ int TPM2_GPIO_Set_Example(void* userCtx, int argc, char *argv[])
102102
/* Write GPIO state */
103103
writeSize = sizeof(pinState);
104104
rc = wolfTPM2_NVWriteAuth(&dev, &nv, nvIndex, &pinState, writeSize, 0);
105-
if (rc != 0) {
105+
if (rc != TPM_RC_SUCCESS) {
106106
printf("Error while setting GPIO state\n");
107107
printf("Make sure GPIO has been configured with './examples/gpio/gpio_config'\n");
108108
goto exit;
@@ -117,7 +117,7 @@ int TPM2_GPIO_Set_Example(void* userCtx, int argc, char *argv[])
117117

118118
exit:
119119

120-
if (rc != 0) {
120+
if (rc != TPM_RC_SUCCESS) {
121121
printf("\nFailure 0x%x: %s\n\n", rc, wolfTPM2_GetRCString(rc));
122122
}
123123

src/tpm2.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5250,6 +5250,7 @@ int TPM2_SetMode(SetMode_In* in)
52505250
}
52515251
#endif /* WOLFTPM_ST33 || WOLFTPM_AUTODETECT */
52525252

5253+
/* GPIO Vendor Specific API's */
52535254
#ifdef WOLFTPM_ST33
52545255
int TPM2_GPIO_Config(GpioConfig_In* in)
52555256
{
@@ -5286,9 +5287,9 @@ int TPM2_GPIO_Config(GpioConfig_In* in)
52865287
}
52875288
return rc;
52885289
}
5289-
#endif /* WOLFTPM_ST33 */
52905290

5291-
#ifdef WOLFTPM_NUVOTON
5291+
#elif defined(WOLFTPM_NUVOTON)
5292+
52925293
int TPM2_NTC2_PreConfig(NTC2_PreConfig_In* in)
52935294
{
52945295
int rc;

0 commit comments

Comments
 (0)