Skip to content

Commit 7a6deaa

Browse files
Merge pull request #383 from dgarske/get_caps
Add support for TPM2_GetCapability for TPM_CAP_PCRS.
2 parents b62a0fd + 180a74e commit 7a6deaa

8 files changed

Lines changed: 270 additions & 36 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ wolftpm/options.h
3838
build/
3939

4040
examples/wrap/wrap_test
41+
examples/wrap/caps
4142
examples/native/native_test
4243
examples/bench/bench
4344
examples/csr/csr

examples/native/native_test.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ int TPM2_Native_TestArgs(void* userCtx, int argc, char *argv[])
158158
} cmdOut;
159159

160160
int pcrCount, pcrIndex, i;
161+
TPML_PCR_SELECTION* pcrSel;
161162
TPML_TAGGED_TPM_PROPERTY* tpmProp;
162163
TPM_HANDLE handle = TPM_RH_NULL;
163164
TPM_HANDLE sessionHandle = TPM_RH_NULL;
@@ -326,6 +327,31 @@ int TPM2_Native_TestArgs(void* userCtx, int argc, char *argv[])
326327
printf("TPM2_GetCapability: Property FIRMWARE_VERSION_2 0x%08x\n",
327328
(unsigned int)tpmProp->tpmProperty[0].value);
328329

330+
/* Get Capability for PCR's */
331+
XMEMSET(&cmdIn.cap, 0, sizeof(cmdIn.cap));
332+
cmdIn.cap.capability = TPM_CAP_PCRS;
333+
cmdIn.cap.property = 0;
334+
cmdIn.cap.propertyCount = 1;
335+
rc = TPM2_GetCapability(&cmdIn.cap, &cmdOut.cap);
336+
if (rc != TPM_RC_SUCCESS) {
337+
printf("TPM2_GetCapability failed 0x%x: %s\n", rc,
338+
TPM2_GetRCString(rc));
339+
goto exit;
340+
}
341+
pcrSel = &cmdOut.cap.capabilityData.data.assignedPCR;
342+
printf("Assigned PCR's:\n");
343+
for (pcrCount=0; pcrCount < (int)pcrSel->count; pcrCount++) {
344+
printf("\t%s: ", TPM2_GetAlgName(pcrSel->pcrSelections[pcrCount].hash));
345+
for (pcrIndex=0;
346+
pcrIndex<pcrSel->pcrSelections[pcrCount].sizeofSelect*8;
347+
pcrIndex++) {
348+
if ((pcrSel->pcrSelections[pcrCount].pcrSelect[pcrIndex/8] &
349+
((1 << (pcrIndex % 8)))) != 0) {
350+
printf(" %d", pcrIndex);
351+
}
352+
}
353+
printf("\n");
354+
}
329355

330356
/* Random */
331357
#if defined(WOLFTPM_ST33) || defined(WOLFTPM_AUTODETECT)

examples/pcr/extend.c

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
static void usage(void)
4848
{
4949
printf("Expected usage:\n");
50-
printf("./examples/pcr/extend [pcr] [filename]\n");
50+
printf("./examples/pcr/extend [-sha1/-sha256/-sha384/-sha512] [pcr] [filename]\n");
5151
printf("* pcr: PCR index between 0-23 (default %d)\n", TPM2_TEST_PCR);
5252
printf("* filename: points to file(data) to measure\n");
5353
printf("\tIf wolfTPM is built with --disable-wolfcrypt the file\n"
@@ -61,18 +61,19 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
6161
{
6262
int i, j, pcrIndex = TPM2_TEST_PCR, rc = -1;
6363
WOLFTPM2_DEV dev;
64+
TPM_ALG_ID alg = TPM_ALG_SHA256;
6465
/* Arbitrary user data provided through a file */
6566
const char *filename = "input.data";
67+
int hashSz;
6668
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES) && \
6769
!defined(WOLFTPM2_NO_WOLFCRYPT)
6870
XFILE fp = NULL;
6971
size_t len;
70-
BYTE hash[TPM_SHA256_DIGEST_SIZE];
71-
#if !defined(NO_SHA256)
72-
/* Using wolfcrypt to hash input data */
72+
BYTE hash[TPM_MAX_DIGEST_SIZE];
73+
7374
BYTE dataBuffer[1024];
74-
wc_Sha256 sha256;
75-
#endif
75+
enum wc_HashType hashType;
76+
wc_HashAlg dig;
7677
#endif
7778

7879
union {
@@ -92,22 +93,43 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
9293
usage();
9394
return 0;
9495
}
96+
}
97+
98+
while (argc > 1) {
99+
if (XSTRCMP(argv[argc-1], "-sha1") == 0) {
100+
alg = TPM_ALG_SHA;
101+
}
102+
else if (XSTRCMP(argv[argc-1], "-sha256") == 0) {
103+
alg = TPM_ALG_SHA256;
104+
}
105+
else if (XSTRCMP(argv[argc-1], "-sha384") == 0) {
106+
alg = TPM_ALG_SHA384;
107+
}
108+
else if (XSTRCMP(argv[argc-1], "-sha512") == 0) {
109+
alg = TPM_ALG_SHA512;
110+
}
95111

96-
/* Advanced usage */
97-
if (argv[1][0] != '-') {
98-
if (pcrIndex < 0 || pcrIndex > 23 || *argv[1] < '0' || *argv[1] > '9') {
112+
else if (*argv[argc-1] >= '0' && *argv[argc-1] <= '9') {
113+
pcrIndex = XATOI(argv[argc-1]);
114+
if (pcrIndex < 0 || pcrIndex > 23) {
99115
printf("PCR index is out of range (0-23)\n");
100116
usage();
101117
return 0;
102118
}
103-
pcrIndex = XATOI(argv[1]);
104119
}
105-
106-
if (argc >= 3 && argv[2][0] != '-')
107-
filename = argv[2];
120+
else if (*argv[argc-1] != '-') {
121+
filename = argv[argc-1];
122+
}
123+
else {
124+
printf("Warning: Unrecognized option: %s\n", argv[argc-1]);
125+
}
126+
argc--;
108127
}
109128

129+
hashSz = TPM2_GetHashDigestSize(alg);
130+
110131
printf("Demo how to extend data into a PCR (TPM2.0 measurement)\n");
132+
printf("\tHash Algorithm: %s (sz %d)\n", TPM2_GetAlgName(alg), hashSz);
111133
printf("\tData file: %s\n", filename);
112134
printf("\tPCR Index: %d\n", pcrIndex);
113135

@@ -122,7 +144,7 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
122144
XMEMSET(&cmdIn.pcrExtend, 0, sizeof(cmdIn.pcrExtend));
123145
cmdIn.pcrExtend.pcrHandle = pcrIndex;
124146
cmdIn.pcrExtend.digests.count = 1;
125-
cmdIn.pcrExtend.digests.digests[0].hashAlg = TPM_ALG_SHA256;
147+
cmdIn.pcrExtend.digests.digests[0].hashAlg = alg;
126148

127149
/* Prepare the hash from user file or predefined value */
128150
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES) && \
@@ -131,36 +153,32 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
131153
fp = XFOPEN(filename, "rb");
132154
}
133155
if (filename && fp != XBADFILE) {
134-
#if !defined(NO_SHA256)
135-
wc_InitSha256(&sha256);
156+
rc = TPM2_GetHashType(alg);
157+
hashType = (enum wc_HashType)rc;
158+
rc = 0;
159+
wc_HashInit(&dig, hashType);
136160
while (!XFEOF(fp)) {
137161
len = XFREAD(dataBuffer, 1, sizeof(dataBuffer), fp);
138162
if (len) {
139-
wc_Sha256Update(&sha256, dataBuffer, (int)len);
163+
wc_HashUpdate(&dig, hashType, dataBuffer, (int)len);
140164
}
141165
}
142-
wc_Sha256Final(&sha256, hash);
143-
#else
144-
len = XFREAD(hash, 1, TPM_SHA256_DIGEST_SIZE, fp);
145-
if (len != TPM_SHA256_DIGEST_SIZE) {
146-
printf("Error while reading SHA256 digest from file.\n");
147-
goto exit;
148-
}
149-
#endif
166+
wc_HashFinal(&dig, hashType, hash);
167+
150168
XMEMCPY(cmdIn.pcrExtend.digests.digests[0].digest.H,
151-
hash, TPM_SHA256_DIGEST_SIZE);
169+
hash, hashSz);
152170
}
153171
else
154172
#endif /* !WOLFTPM2_NO_WOLFCRYPT && !NO_FILESYSTEM */
155173
{
156174
printf("Error loading file %s, using test data\n", filename);
157-
for (i=0; i<TPM_SHA256_DIGEST_SIZE; i++) {
175+
for (i=0; i<hashSz; i++) {
158176
cmdIn.pcrExtend.digests.digests[0].digest.H[i] = i;
159177
}
160178
}
161179

162180
printf("Hash to be used for measurement:\n");
163-
for (i=0; i < TPM_SHA256_DIGEST_SIZE; i++)
181+
for (i=0; i < hashSz; i++)
164182
printf("%02X", cmdIn.pcrExtend.digests.digests[0].digest.H[i]);
165183
printf("\n");
166184

@@ -173,7 +191,7 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
173191
printf("TPM2_PCR_Extend success\n");
174192

175193
XMEMSET(&cmdIn.pcrRead, 0, sizeof(cmdIn.pcrRead));
176-
TPM2_SetupPCRSel(&cmdIn.pcrRead.pcrSelectionIn, TEST_WRAP_DIGEST, pcrIndex);
194+
TPM2_SetupPCRSel(&cmdIn.pcrRead.pcrSelectionIn, alg, pcrIndex);
177195
rc = TPM2_PCR_Read(&cmdIn.pcrRead, &cmdOut.pcrRead);
178196
if (rc != TPM_RC_SUCCESS) {
179197
printf("TPM2_PCR_Read failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));

examples/wrap/caps.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,42 @@ static void usage(void)
4545
{
4646
printf("Expected Usage:\n");
4747
printf("./examples/wrap/caps\n");
48+
}
4849

50+
static int TPM2_PCRs_Print(void)
51+
{
52+
int rc;
53+
int pcrCount, pcrIndex;
54+
GetCapability_In capIn;
55+
GetCapability_Out capOut;
56+
TPML_PCR_SELECTION* pcrSel;
57+
58+
/* List available PCR's */
59+
XMEMSET(&capIn, 0, sizeof(capIn));
60+
capIn.capability = TPM_CAP_PCRS;
61+
capIn.property = 0;
62+
capIn.propertyCount = 1;
63+
rc = TPM2_GetCapability(&capIn, &capOut);
64+
if (rc != TPM_RC_SUCCESS) {
65+
printf("TPM2_GetCapability failed 0x%x: %s\n", rc,
66+
TPM2_GetRCString(rc));
67+
return rc;
68+
}
69+
pcrSel = &capOut.capabilityData.data.assignedPCR;
70+
printf("Assigned PCR's:\n");
71+
for (pcrCount=0; pcrCount < (int)pcrSel->count; pcrCount++) {
72+
printf("\t%s: ", TPM2_GetAlgName(pcrSel->pcrSelections[pcrCount].hash));
73+
for (pcrIndex=0;
74+
pcrIndex<pcrSel->pcrSelections[pcrCount].sizeofSelect*8;
75+
pcrIndex++) {
76+
if ((pcrSel->pcrSelections[pcrCount].pcrSelect[pcrIndex/8] &
77+
((1 << (pcrIndex % 8)))) != 0) {
78+
printf(" %d", pcrIndex);
79+
}
80+
}
81+
printf("\n");
82+
}
83+
return 0;
4984
}
5085

5186
int TPM2_Wrapper_Caps(void* userCtx)
@@ -91,6 +126,9 @@ int TPM2_Wrapper_CapsArgs(void* userCtx, int argc, char *argv[])
91126
printf("Found %d persistent handles\n", rc);
92127
}
93128

129+
/* Print the available PCR's */
130+
TPM2_PCRs_Print();
131+
94132
exit:
95133
wolfTPM2_Shutdown(&dev, 0); /* 0=just shutdown, no startup */
96134

0 commit comments

Comments
 (0)