Skip to content

Commit 523a9f9

Browse files
committed
Fixes for properly setting auth (needs to include name for HMAC cpHash/rpHash calculation). Fix for sessionAttributes when command / response doesn't support it. Fixes for the TLS client / server examples. Added back the useful param enc / hmac debugging enabled with --enable-debug=verbose.
1 parent 75db91c commit 523a9f9

6 files changed

Lines changed: 112 additions & 31 deletions

File tree

examples/tls/tls_client.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,11 @@ int TPM2_TLS_ClientArgs(void* userCtx, int argc, char *argv[])
130130
sockIoCtx.fd = -1;
131131
XMEMSET(&tpmCtx, 0, sizeof(tpmCtx));
132132
#ifndef NO_RSA
133+
XMEMSET(&rsaKey, 0, sizeof(rsaKey));
133134
XMEMSET(&wolfRsaKey, 0, sizeof(wolfRsaKey));
134135
#endif
135136
#ifdef HAVE_ECC
137+
XMEMSET(&eccKey, 0, sizeof(eccKey));
136138
XMEMSET(&wolfEccKey, 0, sizeof(wolfEccKey));
137139
#endif
138140
XMEMSET(&tpmSession, 0, sizeof(tpmSession));
@@ -523,6 +525,7 @@ int TPM2_TLS_ClientArgs(void* userCtx, int argc, char *argv[])
523525
wc_ecc_free(&wolfEccKey);
524526
wolfTPM2_UnloadHandle(&dev, &eccKey.handle);
525527
#endif
528+
wolfTPM2_UnloadHandle(&dev, &tpmSession.handle);
526529

527530
wolfSSL_shutdown(ssl);
528531

examples/tls/tls_server.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,16 @@ int TPM2_TLS_ServerArgs(void* userCtx, int argc, char *argv[])
137137
sockIoCtx.fd = -1;
138138
XMEMSET(&tpmCtx, 0, sizeof(tpmCtx));
139139
#ifndef NO_RSA
140+
XMEMSET(&rsaKey, 0, sizeof(rsaKey));
140141
XMEMSET(&wolfRsaKey, 0, sizeof(wolfRsaKey));
141142
#endif
142143
#ifdef HAVE_ECC
144+
XMEMSET(&eccKey, 0, sizeof(eccKey));
143145
XMEMSET(&wolfEccKey, 0, sizeof(wolfEccKey));
146+
#ifndef WOLFTPM2_USE_SW_ECDHE
147+
/* Ephemeral Key */
148+
XMEMSET(&ecdhKey, 0, sizeof(ecdhKey));
149+
#endif
144150
#endif
145151
XMEMSET(&tpmSession, 0, sizeof(tpmSession));
146152

@@ -236,7 +242,6 @@ int TPM2_TLS_ServerArgs(void* userCtx, int argc, char *argv[])
236242

237243
#ifndef WOLFTPM2_USE_SW_ECDHE
238244
/* Ephemeral Key */
239-
XMEMSET(&ecdhKey, 0, sizeof(ecdhKey));
240245
tpmCtx.ecdhKey = &ecdhKey;
241246
#endif
242247
#endif /* HAVE_ECC */
@@ -497,6 +502,7 @@ int TPM2_TLS_ServerArgs(void* userCtx, int argc, char *argv[])
497502
wc_ecc_free(&wolfEccKey);
498503
wolfTPM2_UnloadHandle(&dev, &eccKey.handle);
499504
#endif
505+
wolfTPM2_UnloadHandle(&dev, &tpmSession.handle);
500506

501507
wolfTPM2_Cleanup(&dev);
502508

examples/wrap/wrap_test.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
121121
TPM_ALG_ID paramEncAlg = TPM_ALG_NULL;
122122
WOLFTPM2_SESSION tpmSession;
123123

124+
XMEMSET(&aesKey, 0, sizeof(aesKey));
125+
XMEMSET(&publicKey, 0, sizeof(publicKey));
124126
#ifndef WOLFTPM2_NO_WOLFCRYPT
125127
#ifndef NO_RSA
126128
XMEMSET(&wolfRsaPubKey, 0, sizeof(wolfRsaPubKey));
@@ -415,6 +417,7 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
415417

416418
/* Close TPM session based on RSA storage key */
417419
wolfTPM2_UnloadHandle(&dev, &tpmSession.handle);
420+
wolfTPM2_SetAuthSession(&dev, 1, NULL, 0); /* clear auth session */
418421

419422

420423
/*------------------------------------------------------------------------*/
@@ -462,6 +465,7 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
462465
storageKey.handle.auth.size);
463466
}
464467

468+
#if 0 /* disabled until ECC Encrypted salt is added */
465469
/* Start an authenticated session (salted / unbound) with parameter encryption */
466470
if (paramEncAlg != TPM_ALG_NULL) {
467471
rc = wolfTPM2_StartSession(&dev, &tpmSession, &storageKey, NULL,
@@ -475,6 +479,7 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
475479
(TPMA_SESSION_decrypt | TPMA_SESSION_encrypt | TPMA_SESSION_continueSession));
476480
if (rc != 0) goto exit;
477481
}
482+
#endif
478483

479484
/* Create an ECC key for ECDSA */
480485
rc = wolfTPM2_GetKeyTemplate_ECC(&publicTemplate,
@@ -630,9 +635,11 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
630635
rc = wolfTPM2_UnloadHandle(&dev, &eccKey.handle);
631636
if (rc != 0) goto exit;
632637

638+
#if 0 /* disabled until ECC Encrypted salt is added */
633639
/* Close TPM session based on ECC storage key */
634640
wolfTPM2_UnloadHandle(&dev, &tpmSession.handle);
635-
641+
wolfTPM2_SetAuthSession(&dev, 1, NULL, 0); /* clear auth session */
642+
#endif
636643

637644
/*------------------------------------------------------------------------*/
638645
/* NV TESTS */
@@ -792,7 +799,6 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
792799
/*------------------------------------------------------------------------*/
793800
/* ENCRYPT/DECRYPT TESTS */
794801
/*------------------------------------------------------------------------*/
795-
XMEMSET(&aesKey, 0, sizeof(aesKey));
796802
rc = wolfTPM2_LoadSymmetricKey(&dev, &aesKey, TEST_AES_MODE,
797803
TEST_AES_KEY, (word32)sizeof(TEST_AES_KEY));
798804
if (rc != 0) goto exit;
@@ -904,6 +910,7 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
904910
wolfTPM2_UnloadHandle(&dev, &rsaKey.handle);
905911
wolfTPM2_UnloadHandle(&dev, &eccKey.handle);
906912
wolfTPM2_UnloadHandle(&dev, &ekKey.handle);
913+
wolfTPM2_UnloadHandle(&dev, &tpmSession.handle);
907914

908915
wolfTPM2_Shutdown(&dev, 0); /* 0=just shutdown, no startup */
909916

src/tpm2.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
175175
}
176176

177177
/* Handle session request for encryption */
178-
if (encParam && session->sessionAttributes & TPMA_SESSION_decrypt) {
178+
if (encParam && authCmd.sessionAttributes & TPMA_SESSION_decrypt) {
179179
/* Encrypt the first command parameter */
180180
rc = TPM2_ParamEnc_CmdRequest(session, encParam, encParamSz);
181181
if (rc != TPM_RC_SUCCESS) {
@@ -210,7 +210,7 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
210210
/* this is done after encryption */
211211
rc = TPM2_CalcHmac(session->authHash, &session->auth, &hash,
212212
&session->nonceCaller, &session->nonceTPM,
213-
session->sessionAttributes, &authCmd.hmac);
213+
authCmd.sessionAttributes, &authCmd.hmac);
214214
if (rc != TPM_RC_SUCCESS) {
215215
#ifdef DEBUG_WOLFTPM
216216
printf("Error calculating command HMAC!\n");
@@ -302,7 +302,7 @@ static int TPM2_ResponseProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
302302
/* Calculate HMAC prior to decryption */
303303
rc = TPM2_CalcHmac(session->authHash, &session->auth, &hash,
304304
&session->nonceTPM, &session->nonceCaller,
305-
session->sessionAttributes, &hmac);
305+
authRsp.sessionAttributes, &hmac);
306306
if (rc != TPM_RC_SUCCESS) {
307307
#ifdef DEBUG_WOLFTPM
308308
printf("Error calculating response HMAC!\n");
@@ -323,7 +323,7 @@ static int TPM2_ResponseProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
323323

324324
/* Handle session request for decryption */
325325
/* If the response supports decryption */
326-
if (decParam && session->sessionAttributes & TPMA_SESSION_encrypt) {
326+
if (decParam && authRsp.sessionAttributes & TPMA_SESSION_encrypt) {
327327
/* Decrypt the first response parameter */
328328
rc = TPM2_ParamDec_CmdResponse(session, decParam, decParamSz);
329329
if (rc != TPM_RC_SUCCESS) {
@@ -5282,6 +5282,10 @@ int TPM2_GetName(TPM2_CTX* ctx, int handleCnt, int idx, TPM2B_NAME* name)
52825282
name->size = session->name.size;
52835283
XMEMCPY(name->name, session->name.name, name->size);
52845284
}
5285+
#ifdef WOLFTPM_DEBUG_VERBOSE
5286+
printf("Name %d: %d\n", idx, name->size);
5287+
TPM2_PrintBin(name->name, name->size);
5288+
#endif
52855289
return TPM_RC_SUCCESS;
52865290
}
52875291

src/tpm2_param_enc.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ static int TPM2_ParamEnc_AESCFB(TPM2_AUTH_SESSION *session, TPM2B_AUTH* keyIn,
286286
return TPM_RC_FAILURE;
287287
}
288288

289+
#ifdef WOLFTPM_DEBUG_VERBOSE
290+
printf("AES Enc Key %d, IV %d\n", symKeySz, symKeyIvSz);
291+
TPM2_PrintBin(symKey, symKeySz);
292+
TPM2_PrintBin(&symKey[symKeySz], symKeyIvSz);
293+
#endif
294+
289295
/* Perform AES CFB Encryption */
290296
rc = wc_AesInit(&enc, NULL, INVALID_DEVID);
291297
if (rc == 0) {
@@ -325,6 +331,12 @@ static int TPM2_ParamDec_AESCFB(TPM2_AUTH_SESSION *session, TPM2B_AUTH* keyIn,
325331
return TPM_RC_FAILURE;
326332
}
327333

334+
#ifdef WOLFTPM_DEBUG_VERBOSE
335+
printf("AES Dec Key %d, IV %d\n", symKeySz, symKeyIvSz);
336+
TPM2_PrintBin(symKey, symKeySz);
337+
TPM2_PrintBin(&symKey[symKeySz], symKeyIvSz);
338+
#endif
339+
328340
/* Perform AES CFB Decryption */
329341
rc = wc_AesInit(&dec, NULL, INVALID_DEVID);
330342
if (rc == 0) {
@@ -386,6 +398,11 @@ int TPM2_CalcCpHash(TPMI_ALG_HASH authHash, TPM_CC cmdCode,
386398
wc_HashFree(&hash_ctx, hashType);
387399
}
388400

401+
#ifdef WOLFTPM_DEBUG_VERBOSE
402+
printf("cpHash: cmd %x, size %d\n", cmdCode, hash->size);
403+
TPM2_PrintBin(hash->buffer, hash->size);
404+
#endif
405+
389406
return rc;
390407
}
391408

@@ -430,6 +447,11 @@ int TPM2_CalcRpHash(TPMI_ALG_HASH authHash,
430447
wc_HashFree(&hash_ctx, hashType);
431448
}
432449

450+
#ifdef WOLFTPM_DEBUG_VERBOSE
451+
printf("rpHash: cmd %x, size %d\n", cmdCode, hash->size);
452+
TPM2_PrintBin(hash->buffer, hash->size);
453+
#endif
454+
433455
return rc;
434456
}
435457

@@ -455,9 +477,14 @@ int TPM2_CalcHmac(TPMI_ALG_HASH authHash, TPM2B_AUTH* auth,
455477
rc = wc_HmacInit(&hmac_ctx, NULL, INVALID_DEVID);
456478
if (rc != 0)
457479
return rc;
480+
458481
/* start HMAC - sessionKey || authValue */
459482
/* TODO: Handle "authValue" case "a value that is found in the sensitive area of an entity" */
460483
if (auth) {
484+
#ifdef WOLFTPM_DEBUG_VERBOSE
485+
printf("HMAC Key: %d\n", auth->size);
486+
TPM2_PrintBin(auth->buffer, auth->size);
487+
#endif
461488
rc = wc_HmacSetKey(&hmac_ctx, hashType, auth->buffer, auth->size);
462489
}
463490
else {
@@ -488,6 +515,11 @@ int TPM2_CalcHmac(TPMI_ALG_HASH authHash, TPM2B_AUTH* auth,
488515
rc = wc_HmacFinal(&hmac_ctx, hmac->buffer);
489516
wc_HmacFree(&hmac_ctx);
490517

518+
#ifdef WOLFTPM_DEBUG_VERBOSE
519+
printf("HMAC Auth: attrib %x, size %d\n", sessionAttributes, hmac->size);
520+
TPM2_PrintBin(hmac->buffer, hmac->size);
521+
#endif
522+
491523
return rc;
492524
}
493525
#endif /* !WOLFTPM2_NO_WOLFCRYPT */
@@ -497,6 +529,16 @@ TPM_RC TPM2_ParamEnc_CmdRequest(TPM2_AUTH_SESSION *session,
497529
{
498530
TPM_RC rc = TPM_RC_FAILURE;
499531

532+
#ifdef WOLFTPM_DEBUG_VERBOSE
533+
printf("CmdEnc Session Key %d\n", session->auth.size);
534+
TPM2_PrintBin(session->auth.buffer, session->auth.size);
535+
printf("CmdEnc Nonce caller %d\n", session->nonceCaller.size);
536+
TPM2_PrintBin(session->nonceCaller.buffer, session->nonceCaller.size);
537+
printf("CmdEnc Nonce TPM %d\n", session->nonceTPM.size);
538+
TPM2_PrintBin(session->nonceTPM.buffer, session->nonceTPM.size);
539+
#endif
540+
541+
500542
if (session->symmetric.algorithm == TPM_ALG_XOR) {
501543
rc = TPM2_ParamEnc_XOR(session, &session->auth, &session->nonceCaller,
502544
&session->nonceTPM, paramData, paramSz);
@@ -517,6 +559,15 @@ TPM_RC TPM2_ParamDec_CmdResponse(TPM2_AUTH_SESSION *session,
517559
{
518560
TPM_RC rc = TPM_RC_FAILURE;
519561

562+
#ifdef WOLFTPM_DEBUG_VERBOSE
563+
printf("RspDec Session Key %d\n", session->auth.size);
564+
TPM2_PrintBin(session->auth.buffer, session->auth.size);
565+
printf("RspDec Nonce caller %d\n", session->nonceCaller.size);
566+
TPM2_PrintBin(session->nonceCaller.buffer, session->nonceCaller.size);
567+
printf("RspDec Nonce TPM %d\n", session->nonceTPM.size);
568+
TPM2_PrintBin(session->nonceTPM.buffer, session->nonceTPM.size);
569+
#endif
570+
520571
if (session->symmetric.algorithm == TPM_ALG_XOR) {
521572
rc = TPM2_ParamDec_XOR(session, &session->auth, &session->nonceCaller,
522573
&session->nonceTPM, paramData, paramSz);

0 commit comments

Comments
 (0)