Skip to content

Commit

Permalink
Merge pull request #198 from dgarske/pcr_select
Browse files Browse the repository at this point in the history
  • Loading branch information
elms authored Dec 8, 2021
2 parents fa39826 + eebd30a commit db04fe6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 39 deletions.
21 changes: 8 additions & 13 deletions examples/pcr/extend.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static void usage(void)

int TPM2_Extend_Test(void* userCtx, int argc, char *argv[])
{
int i, pcrIndex = TPM2_TEST_PCR, rc = -1;
int i, j, pcrIndex = TPM2_TEST_PCR, rc = -1;
WOLFTPM2_DEV dev;
/* Arbitrary user data provided through a file */
const char *filename = "input.data";
Expand All @@ -72,18 +72,14 @@ int TPM2_Extend_Test(void* userCtx, int argc, char *argv[])
#endif

union {
#ifdef DEBUG_WOLFTPM
PCR_Read_In pcrRead;
#endif
PCR_Extend_In pcrExtend;
byte maxInput[MAX_COMMAND_SIZE];
} cmdIn;
#ifdef DEBUG_WOLFTPM
union {
PCR_Read_Out pcrRead;
byte maxOutput[MAX_RESPONSE_SIZE];
} cmdOut;
#endif

if (argc >= 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
Expand Down Expand Up @@ -171,20 +167,19 @@ int TPM2_Extend_Test(void* userCtx, int argc, char *argv[])
}
printf("TPM2_PCR_Extend success\n");

#ifdef DEBUG_WOLFTPM
XMEMSET(&cmdIn.pcrRead, 0, sizeof(cmdIn.pcrRead));
TPM2_SetupPCRSel(&cmdIn.pcrRead.pcrSelectionIn,
TEST_WRAP_DIGEST, pcrIndex);
TPM2_SetupPCRSel(&cmdIn.pcrRead.pcrSelectionIn, TEST_WRAP_DIGEST, pcrIndex);
rc = TPM2_PCR_Read(&cmdIn.pcrRead, &cmdOut.pcrRead);
if (rc != TPM_RC_SUCCESS) {
printf("TPM2_PCR_Read failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

printf("PCR%d digest:\n", pcrIndex);
TPM2_PrintBin(cmdOut.pcrRead.pcrValues.digests[0].buffer,
cmdOut.pcrRead.pcrValues.digests[0].size);
#endif
for (i=0; i < (int)cmdOut.pcrRead.pcrValues.count; i++) {
printf("PCR%d (idx %d) digest:\n", pcrIndex, i);
for (j=0; j < cmdOut.pcrRead.pcrValues.digests[i].size; j++)
printf("%02X", cmdOut.pcrRead.pcrValues.digests[i].buffer[j]);
printf("\n");
}

exit:

Expand Down
30 changes: 18 additions & 12 deletions examples/pcr/read_pcr.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,17 @@ static void usage(void)

int TPM2_Read_Test(void* userCtx, int argc, char *argv[])
{
int rc = -1;
int rc = -1, i, j;
int pcrIndex = TPM2_TEST_PCR;
WOLFTPM2_DEV dev;
TPM2B_DIGEST pcrValue;
union {
PCR_Read_In pcrRead;
byte maxInput[MAX_COMMAND_SIZE];
} cmdIn;
union {
PCR_Read_Out pcrRead;
byte maxOutput[MAX_RESPONSE_SIZE];
} cmdOut;

if (argc >= 2) {
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
Expand Down Expand Up @@ -85,23 +92,22 @@ int TPM2_Read_Test(void* userCtx, int argc, char *argv[])
}
printf("wolfTPM2_Init: success\n");

/*

XMEMSET(&cmdIn.pcrRead, 0, sizeof(cmdIn.pcrRead));
TPM2_SetupPCRSel(&cmdIn.pcrRead.pcrSelectionIn,
TEST_WRAP_DIGEST, pcrIndex);
/* Note: TPM2_SetupPCRSel can be called with multiple PCR indexes to
* read more than one at a time */
TPM2_SetupPCRSel(&cmdIn.pcrRead.pcrSelectionIn, TEST_WRAP_DIGEST, pcrIndex);
rc = TPM2_PCR_Read(&cmdIn.pcrRead, &cmdOut.pcrRead);
if (rc != TPM_RC_SUCCESS) {
printf("TPM2_PCR_Read failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}
*/

rc = wolfTPM2_ReadPCR(&dev, pcrIndex, TEST_WRAP_DIGEST, pcrValue.buffer, (int*)&pcrValue.size);
if (rc != TPM_RC_SUCCESS ) {
printf("wolfTPM2_PCR_Read failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
for (i=0; i < (int)cmdOut.pcrRead.pcrValues.count; i++) {
printf("PCR%d (idx %d) digest:\n", pcrIndex, i);
for (j=0; j < cmdOut.pcrRead.pcrValues.digests[i].size; j++)
printf("%02X", cmdOut.pcrRead.pcrValues.digests[i].buffer[j]);
printf("\n");
}
printf("wolfTPM2_PCR_Read: success\n");

exit:

Expand Down
21 changes: 8 additions & 13 deletions examples/pcr/reset.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,18 @@ static void usage(void)

int TPM2_Reset_Test(void* userCtx, int argc, char *argv[])
{
int pcrIndex = TPM2_TEST_PCR, rc = -1;
int i, j, pcrIndex = TPM2_TEST_PCR, rc = -1;
WOLFTPM2_DEV dev;

union {
#ifdef DEBUG_WOLFTPM
PCR_Read_In pcrRead;
#endif
PCR_Reset_In pcrReset;
byte maxInput[MAX_COMMAND_SIZE];
} cmdIn;
#ifdef DEBUG_WOLFTPM
union {
PCR_Read_Out pcrRead;
byte maxOutput[MAX_RESPONSE_SIZE];
} cmdOut;
#endif

if (argc == 2) {
pcrIndex = atoi(argv[1]);
Expand Down Expand Up @@ -101,20 +97,19 @@ int TPM2_Reset_Test(void* userCtx, int argc, char *argv[])
}
printf("TPM2_PCR_Reset success\n");

#ifdef DEBUG_WOLFTPM
XMEMSET(&cmdIn.pcrRead, 0, sizeof(cmdIn.pcrRead));
TPM2_SetupPCRSel(&cmdIn.pcrRead.pcrSelectionIn,
TEST_WRAP_DIGEST, pcrIndex);
TPM2_SetupPCRSel(&cmdIn.pcrRead.pcrSelectionIn, TEST_WRAP_DIGEST, pcrIndex);
rc = TPM2_PCR_Read(&cmdIn.pcrRead, &cmdOut.pcrRead);
if (rc != TPM_RC_SUCCESS) {
printf("TPM2_PCR_Read failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

printf("PCR%d digest:\n", pcrIndex);
TPM2_PrintBin(cmdOut.pcrRead.pcrValues.digests[0].buffer,
cmdOut.pcrRead.pcrValues.digests[0].size);
#endif
for (i=0; i < (int)cmdOut.pcrRead.pcrValues.count; i++) {
printf("PCR%d (idx %d) digest:\n", pcrIndex, i);
for (j=0; j < cmdOut.pcrRead.pcrValues.digests[i].size; j++)
printf("%02X", cmdOut.pcrRead.pcrValues.digests[i].buffer[j]);
printf("\n");
}

exit:

Expand Down
1 change: 0 additions & 1 deletion src/tpm2.c
Original file line number Diff line number Diff line change
Expand Up @@ -5476,7 +5476,6 @@ void TPM2_SetupPCRSel(TPML_PCR_SELECTION* pcr, TPM_ALG_ID alg, int pcrIndex)
pcr->count = 1;
pcr->pcrSelections[0].hash = alg;
pcr->pcrSelections[0].sizeofSelect = PCR_SELECT_MIN;
XMEMSET(pcr->pcrSelections[0].pcrSelect, 0, PCR_SELECT_MIN);
pcr->pcrSelections[0].pcrSelect[pcrIndex >> 3] = (1 << (pcrIndex & 0x7));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tpm2_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2713,6 +2713,7 @@ int wolfTPM2_ReadPCR(WOLFTPM2_DEV* dev, int pcrIndex, int hashAlg, byte* digest,
wolfTPM2_SetAuthPassword(dev, 0, NULL);
}

XMEMSET(&pcrReadIn, 0, sizeof(pcrReadIn));
wolfTPM2_SetupPCRSel(&pcrReadIn.pcrSelectionIn, hashAlg, pcrIndex);
rc = TPM2_PCR_Read(&pcrReadIn, &pcrReadOut);
if (rc != TPM_RC_SUCCESS) {
Expand Down

0 comments on commit db04fe6

Please sign in to comment.