Skip to content

Commit 79105f9

Browse files
authored
Merge pull request #305 from dgarske/auth_refactor
TPM command authentication refactor
2 parents ddbf4ef + cdcdf38 commit 79105f9

File tree

5 files changed

+292
-272
lines changed

5 files changed

+292
-272
lines changed

examples/nvram/store.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ int TPM2_NVRAM_Store_Example(void* userCtx, int argc, char *argv[])
7373
byte pubAreaBuffer[sizeof(TPM2B_PUBLIC)];
7474
int pubAreaSize;
7575
word32 nvIndex = TPM2_DEMO_NVRAM_STORE_INDEX;
76+
byte* auth = (byte*)gNvAuth;
77+
word32 authSz = (word32)sizeof(gNvAuth)-1;
7678

7779
if (argc >= 2) {
7880
if (XSTRCMP(argv[1], "-?") == 0 ||
@@ -133,6 +135,7 @@ int TPM2_NVRAM_Store_Example(void* userCtx, int argc, char *argv[])
133135
printf("Parameter Encryption: Not enabled (try -aes or -xor).\n\n");
134136
}
135137

138+
XMEMSET(&nv, 0, sizeof(nv));
136139
XMEMSET(&keyBlob, 0, sizeof(keyBlob));
137140
XMEMSET(&tpmSession, 0, sizeof(tpmSession));
138141
XMEMSET(&parent, 0, sizeof(parent));
@@ -164,11 +167,17 @@ int TPM2_NVRAM_Store_Example(void* userCtx, int argc, char *argv[])
164167
rc = wolfTPM2_GetNvAttributesTemplate(parent.hndl, &nvAttributes);
165168
if (rc != 0) goto exit;
166169

167-
/* Our wolfTPM2 wrapper for NV_Define */
168-
rc = wolfTPM2_NVCreateAuth(&dev, &parent, &nv, nvIndex,
169-
nvAttributes, TPM2_DEMO_NV_TEST_SIZE, (byte*)gNvAuth, sizeof(gNvAuth)-1);
170-
if (rc != 0 && rc != TPM_RC_NV_DEFINED) goto exit;
170+
/* Try and open existing NV */
171+
rc = wolfTPM2_NVOpen(&dev, &nv, nvIndex, auth, authSz);
172+
if (rc != 0) {
173+
/* In not found try create using wolfTPM2 wrapper for NV_Define */
174+
rc = wolfTPM2_NVCreateAuth(&dev, &parent, &nv, nvIndex,
175+
nvAttributes, TPM2_DEMO_NV_TEST_SIZE, auth, authSz);
171176

177+
if (rc != 0 && rc != TPM_RC_NV_DEFINED) goto exit;
178+
}
179+
/* The set auth is done already in NVOpen and NVCreateAuth, but shown here
180+
* as example for how to set the authentication on a handle */
172181
wolfTPM2_SetAuthHandle(&dev, 0, &nv.handle);
173182

174183
printf("Storing key at TPM NV index 0x%x with password protection\n\n",

0 commit comments

Comments
 (0)