Skip to content

Commit 75db91c

Browse files
committed
Updates to demonstrate using parameter encryption with the native test. Allow NULL key for the HmacSetKey (for unsalted / unbound).
1 parent d044360 commit 75db91c

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

examples/native/native_test.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
/* This example shows using the TPM2_ specification API's in TPM2_Native_Test() */
2323

2424
#include <wolftpm/tpm2.h>
25+
#include <wolftpm/tpm2_param_enc.h>
2526

2627
#include <examples/native/native_test.h>
2728
#include <examples/tpm_io.h>
@@ -178,6 +179,9 @@ int TPM2_Native_TestArgs(void* userCtx, int argc, char *argv[])
178179
"\x06\xC1";
179180

180181
TPM2_AUTH_SESSION session[MAX_SESSION_NUM];
182+
#ifndef WOLFTPM2_NO_WOLFCRYPT
183+
TPM2B_AUTH sessionAuth;
184+
#endif
181185

182186
(void)argc;
183187
(void)argv;
@@ -454,7 +458,13 @@ int TPM2_Native_TestArgs(void* userCtx, int argc, char *argv[])
454458
cmdIn.authSes.tpmKey = TPM_RH_NULL;
455459
cmdIn.authSes.bind = TPM_RH_NULL;
456460
cmdIn.authSes.sessionType = TPM_SE_POLICY;
461+
#ifndef WOLFTPM2_NO_WOLFCRYPT
462+
cmdIn.authSes.symmetric.algorithm = TPM_ALG_AES;
463+
cmdIn.authSes.symmetric.keyBits.aes = 128;
464+
cmdIn.authSes.symmetric.mode.aes = TPM_ALG_CFB;
465+
#else
457466
cmdIn.authSes.symmetric.algorithm = TPM_ALG_NULL;
467+
#endif
458468
cmdIn.authSes.authHash = TPM_ALG_SHA256;
459469
cmdIn.authSes.nonceCaller.size = TPM_SHA256_DIGEST_SIZE;
460470
rc = TPM2_GetNonce(cmdIn.authSes.nonceCaller.buffer,
@@ -471,8 +481,22 @@ int TPM2_Native_TestArgs(void* userCtx, int argc, char *argv[])
471481
goto exit;
472482
}
473483
sessionHandle = cmdOut.authSes.sessionHandle;
474-
printf("TPM2_StartAuthSession: sessionHandle 0x%x\n", (word32)sessionHandle);
484+
session[0].nonceTPM = cmdOut.authSes.nonceTPM;
475485

486+
#ifndef WOLFTPM2_NO_WOLFCRYPT
487+
/* calculate session key */
488+
sessionAuth.size = TPM2_GetHashDigestSize(cmdIn.authSes.authHash);
489+
rc = TPM2_KDFa(cmdIn.authSes.authHash, NULL, "ATH",
490+
&cmdOut.authSes.nonceTPM, &cmdIn.authSes.nonceCaller,
491+
sessionAuth.buffer, sessionAuth.size);
492+
if (rc != sessionAuth.size) {
493+
printf("KDFa ATH Gen Error %d\n", rc);
494+
rc = TPM_RC_FAILURE;
495+
goto exit;
496+
}
497+
rc = TPM_RC_SUCCESS;
498+
#endif
499+
printf("TPM2_StartAuthSession: sessionHandle 0x%x\n", (word32)sessionHandle);
476500

477501
/* Policy Get Digest */
478502
XMEMSET(&cmdIn.policyGetDigest, 0, sizeof(cmdIn.policyGetDigest));
@@ -515,6 +539,17 @@ int TPM2_Native_TestArgs(void* userCtx, int argc, char *argv[])
515539
printf("wc_Hash of PCR[0]: size %d\n", hash_len);
516540
TPM2_PrintBin(hash, hash_len);
517541

542+
/* Set Auth Session index 0 */
543+
session[0].sessionHandle = sessionHandle;
544+
session[0].sessionAttributes = (TPMA_SESSION_decrypt | TPMA_SESSION_encrypt |
545+
TPMA_SESSION_continueSession);
546+
session[0].authHash = WOLFTPM2_WRAP_DIGEST;
547+
session[0].symmetric.algorithm = TPM_ALG_AES;
548+
session[0].symmetric.keyBits.aes = 128;
549+
session[0].symmetric.mode.aes = TPM_ALG_CFB;
550+
session[0].nonceCaller.size = TPM2_GetHashDigestSize(WOLFTPM2_WRAP_DIGEST);
551+
session[0].auth = sessionAuth;
552+
518553
/* Policy PCR */
519554
pcrIndex = 0;
520555
XMEMSET(&cmdIn.policyPCR, 0, sizeof(cmdIn.policyPCR));
@@ -531,6 +566,8 @@ int TPM2_Native_TestArgs(void* userCtx, int argc, char *argv[])
531566
else {
532567
printf("TPM2_PolicyPCR: Updated\n");
533568
}
569+
XMEMSET(&session[0], 0, sizeof(TPM2_AUTH_SESSION));
570+
session[0].sessionHandle = TPM_RS_PW;
534571
#endif
535572

536573
/* Policy Restart (for session) */

src/tpm2_param_enc.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ int TPM2_KDFa(
120120
counter++;
121121

122122
/* start HMAC */
123-
ret = wc_HmacSetKey(&hmac_ctx, hashType, keyIn->buffer, keyIn->size);
123+
if (keyIn) {
124+
ret = wc_HmacSetKey(&hmac_ctx, hashType, keyIn->buffer, keyIn->size);
125+
}
126+
else {
127+
ret = wc_HmacSetKey(&hmac_ctx, hashType, NULL, 0);
128+
}
124129
if (ret != 0)
125130
goto exit;
126131

@@ -452,7 +457,12 @@ int TPM2_CalcHmac(TPMI_ALG_HASH authHash, TPM2B_AUTH* auth,
452457
return rc;
453458
/* start HMAC - sessionKey || authValue */
454459
/* TODO: Handle "authValue" case "a value that is found in the sensitive area of an entity" */
455-
rc = wc_HmacSetKey(&hmac_ctx, hashType, auth->buffer, auth->size);
460+
if (auth) {
461+
rc = wc_HmacSetKey(&hmac_ctx, hashType, auth->buffer, auth->size);
462+
}
463+
else {
464+
rc = wc_HmacSetKey(&hmac_ctx, hashType, NULL, 0);
465+
}
456466

457467
/* pHash - hash of command code and parameters */
458468
if (rc == 0)

0 commit comments

Comments
 (0)