Skip to content

Commit 7411bc1

Browse files
committed
Refactor the TPM2_GetNonce to support a non-locking version for internal use. This avoids all possible recursive mutex calls.
1 parent 4214ffa commit 7411bc1

4 files changed

Lines changed: 72 additions & 47 deletions

File tree

examples/run_examples.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ run_tpm_tls_client() { # Usage: run_tpm_tls_client [ecc/rsa] [tpmargs] [tlsversi
378378
generate_port
379379
pushd $WOLFSSL_PATH >> $TPMPWD/run.out 2>&1
380380
echo -e "./examples/server/server -v $3 -p $port -w -g -A ./certs/tpm-ca-$1-cert.pem"
381-
./examples/server/server -p $port -w -g -A ./certs/tpm-ca-$1-cert.pem &> $TPMPWD/run.out &
381+
./examples/server/server -p $port -w -g -A ./certs/tpm-ca-$1-cert.pem >> $TPMPWD/run.out 2>&1 &
382382
RESULT=$?
383383
[ $RESULT -ne 0 ] && echo -e "tls server $1 $2 failed! $RESULT" && exit 1
384384
popd >> $TPMPWD/run.out 2>&1

src/tpm2.c

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
163163

164164
if (session->sessionHandle != TPM_RS_PW) {
165165
/* Generate fresh nonce */
166-
rc = TPM2_GetNonce(session->nonceCaller.buffer,
166+
rc = TPM2_GetNonceNoLock(session->nonceCaller.buffer,
167167
session->nonceCaller.size);
168168
if (rc != TPM_RC_SUCCESS) {
169169
return rc;
@@ -5689,9 +5689,7 @@ int TPM2_GetHashType(TPMI_ALG_HASH hashAlg)
56895689
return 0;
56905690
}
56915691

5692-
/* Can optionally define WOLFTPM2_USE_HW_RNG to force using TPM hardware for
5693-
* RNG source */
5694-
int TPM2_GetNonce(byte* nonceBuf, int nonceSz)
5692+
int TPM2_GetNonceNoLock(byte* nonceBuf, int nonceSz)
56955693
{
56965694
int rc;
56975695
TPM2_CTX* ctx = TPM2_GetActiveCtx();
@@ -5720,44 +5718,58 @@ int TPM2_GetNonce(byte* nonceBuf, int nonceSz)
57205718
#else
57215719
/* Call GetRandom directly, so a custom packet buffer can be used.
57225720
* This won't conflict when being called from TPM2_CommandProcess. */
5723-
rc = TPM2_AcquireLock(ctx);
5724-
if (rc == TPM_RC_SUCCESS) {
5725-
while (randSz < nonceSz) {
5726-
UINT16 inSz = nonceSz - randSz, outSz = 0;
5727-
if (inSz > MAX_RNG_REQ_SIZE) {
5728-
inSz = MAX_RNG_REQ_SIZE;
5729-
}
5721+
while (randSz < nonceSz) {
5722+
UINT16 inSz = nonceSz - randSz, outSz = 0;
5723+
if (inSz > MAX_RNG_REQ_SIZE) {
5724+
inSz = MAX_RNG_REQ_SIZE;
5725+
}
57305726

5731-
TPM2_Packet_InitBuf(&packet, buffer, (int)sizeof(buffer));
5732-
TPM2_Packet_AppendU16(&packet, inSz);
5733-
TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_GetRandom);
5734-
rc = TPM2_SendCommand(ctx, &packet);
5735-
#ifdef WOLFTPM_DEBUG_VERBOSE
5736-
printf("TPM2_GetNonce (%d bytes at %d): %d (%s)\n",
5737-
inSz, randSz, rc, TPM2_GetRCString(rc));
5738-
#endif
5739-
if (rc != TPM_RC_SUCCESS) {
5740-
break;
5741-
}
5727+
TPM2_Packet_InitBuf(&packet, buffer, (int)sizeof(buffer));
5728+
TPM2_Packet_AppendU16(&packet, inSz);
5729+
TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_GetRandom);
5730+
rc = TPM2_SendCommand(ctx, &packet);
5731+
#ifdef WOLFTPM_DEBUG_VERBOSE
5732+
printf("TPM2_GetNonce (%d bytes at %d): %d (%s)\n",
5733+
inSz, randSz, rc, TPM2_GetRCString(rc));
5734+
#endif
5735+
if (rc != TPM_RC_SUCCESS) {
5736+
break;
5737+
}
57425738

5743-
TPM2_Packet_ParseU16(&packet, &outSz);
5744-
if (outSz > MAX_RNG_REQ_SIZE) {
5745-
#ifdef DEBUG_WOLFTPM
5746-
printf("TPM2_GetNonce out size error\n");
5747-
#endif
5748-
rc = BAD_FUNC_ARG;
5749-
break;
5750-
}
5751-
TPM2_Packet_ParseBytes(&packet, &nonceBuf[randSz], outSz);
5752-
randSz += outSz;
5739+
TPM2_Packet_ParseU16(&packet, &outSz);
5740+
if (outSz > MAX_RNG_REQ_SIZE) {
5741+
#ifdef DEBUG_WOLFTPM
5742+
printf("TPM2_GetNonce out size error\n");
5743+
#endif
5744+
rc = BAD_FUNC_ARG;
5745+
break;
57535746
}
5754-
TPM2_ReleaseLock(ctx);
5747+
TPM2_Packet_ParseBytes(&packet, &nonceBuf[randSz], outSz);
5748+
randSz += outSz;
57555749
}
57565750
#endif
57575751

57585752
return rc;
57595753
}
57605754

5755+
int TPM2_GetNonce(byte* nonceBuf, int nonceSz)
5756+
{
5757+
int rc;
5758+
TPM2_CTX* ctx = TPM2_GetActiveCtx();
5759+
5760+
if (ctx == NULL) {
5761+
return BAD_FUNC_ARG;
5762+
}
5763+
5764+
rc = TPM2_AcquireLock(ctx);
5765+
if (rc == TPM_RC_SUCCESS) {
5766+
rc = TPM2_GetNonceNoLock(nonceBuf, nonceSz);
5767+
TPM2_ReleaseLock(ctx);
5768+
}
5769+
5770+
return rc;
5771+
}
5772+
57615773
/* Get name for object/handle */
57625774
int TPM2_GetName(TPM2_CTX* ctx, UINT32 handleValue, int handleCnt, int idx, TPM2B_NAME* name)
57635775
{

src/tpm2_wrap.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session,
15921592
authSesIn.symmetric.algorithm = TPM_ALG_NULL;
15931593
}
15941594
authSesIn.nonceCaller.size = hashDigestSz;
1595-
rc = TPM2_GetNonce(authSesIn.nonceCaller.buffer,
1595+
rc = TPM2_GetNonceNoLock(authSesIn.nonceCaller.buffer,
15961596
authSesIn.nonceCaller.size);
15971597
if (rc < 0) {
15981598
#ifdef DEBUG_WOLFTPM
@@ -1604,7 +1604,7 @@ int wolfTPM2_StartSession(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session,
16041604
if (authSesIn.tpmKey != TPM_RH_NULL) {
16051605
/* Generate random salt */
16061606
session->salt.size = hashDigestSz;
1607-
rc = TPM2_GetNonce(session->salt.buffer, session->salt.size);
1607+
rc = TPM2_GetNonceNoLock(session->salt.buffer, session->salt.size);
16081608
if (rc != 0) {
16091609
return rc;
16101610
}
@@ -2481,6 +2481,7 @@ int wolfTPM2_ImportRsaPrivateKeySeed(WOLFTPM2_DEV* dev,
24812481
TPMI_ALG_RSA_SCHEME scheme, TPMI_ALG_HASH hashAlg, TPMA_OBJECT attributes,
24822482
byte* seed, word32 seedSz)
24832483
{
2484+
int rc = 0;
24842485
TPM2B_PUBLIC pub;
24852486
TPM2B_SENSITIVE sens;
24862487
word32 digestSz;
@@ -2544,11 +2545,13 @@ int wolfTPM2_ImportRsaPrivateKeySeed(WOLFTPM2_DEV* dev,
25442545
else {
25452546
/* assign random seed */
25462547
sens.sensitiveArea.seedValue.size = digestSz;
2547-
TPM2_GetNonce(sens.sensitiveArea.seedValue.buffer,
2548+
rc = TPM2_GetNonceNoLock(sens.sensitiveArea.seedValue.buffer,
25482549
sens.sensitiveArea.seedValue.size);
25492550
}
2550-
2551-
return wolfTPM2_ImportPrivateKey(dev, parentKey, keyBlob, &pub, &sens);
2551+
if (rc == 0) {
2552+
rc = wolfTPM2_ImportPrivateKey(dev, parentKey, keyBlob, &pub, &sens);
2553+
}
2554+
return rc;
25522555
}
25532556
int wolfTPM2_ImportRsaPrivateKey(WOLFTPM2_DEV* dev,
25542557
const WOLFTPM2_KEY* parentKey, WOLFTPM2_KEYBLOB* keyBlob, const byte* rsaPub,
@@ -2633,6 +2636,7 @@ int wolfTPM2_ImportEccPrivateKeySeed(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* pare
26332636
const byte* eccPriv, word32 eccPrivSz,
26342637
TPMA_OBJECT attributes, byte* seed, word32 seedSz)
26352638
{
2639+
int rc = 0;
26362640
TPM2B_PUBLIC pub;
26372641
TPM2B_SENSITIVE sens;
26382642
word32 digestSz;
@@ -2696,11 +2700,14 @@ int wolfTPM2_ImportEccPrivateKeySeed(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* pare
26962700
else {
26972701
/* assign random seed */
26982702
sens.sensitiveArea.seedValue.size = digestSz;
2699-
TPM2_GetNonce(sens.sensitiveArea.seedValue.buffer,
2703+
rc = TPM2_GetNonceNoLock(sens.sensitiveArea.seedValue.buffer,
27002704
sens.sensitiveArea.seedValue.size);
27012705
}
27022706

2703-
return wolfTPM2_ImportPrivateKey(dev, parentKey, keyBlob, &pub, &sens);
2707+
if (rc == 0) {
2708+
rc = wolfTPM2_ImportPrivateKey(dev, parentKey, keyBlob, &pub, &sens);
2709+
}
2710+
return rc;
27042711
}
27052712

27062713
int wolfTPM2_ImportEccPrivateKey(WOLFTPM2_DEV* dev, const WOLFTPM2_KEY* parentKey,
@@ -3234,13 +3241,14 @@ int wolfTPM2_ImportPrivateKeyBuffer(WOLFTPM2_DEV* dev,
32343241
else {
32353242
/* assign random seed */
32363243
sens.sensitiveArea.seedValue.size = digestSz;
3237-
TPM2_GetNonce(sens.sensitiveArea.seedValue.buffer,
3244+
rc = TPM2_GetNonceNoLock(sens.sensitiveArea.seedValue.buffer,
32383245
sens.sensitiveArea.seedValue.size);
32393246
}
32403247

3241-
3242-
/* Import Private Key */
3243-
rc = wolfTPM2_ImportPrivateKey(dev, parentKey, keyBlob, pub, &sens);
3248+
if (rc == 0) {
3249+
/* Import Private Key */
3250+
rc = wolfTPM2_ImportPrivateKey(dev, parentKey, keyBlob, pub, &sens);
3251+
}
32443252
}
32453253

32463254
#ifdef WOLFTPM2_PEM_DECODE
@@ -5776,7 +5784,7 @@ int wolfTPM2_ChangeHierarchyAuth(WOLFTPM2_DEV* dev, WOLFTPM2_SESSION* session,
57765784
}
57775785
}
57785786
if (rc == 0) {
5779-
rc = TPM2_GetNonce(in.newAuth.buffer, in.newAuth.size);
5787+
rc = TPM2_GetNonceNoLock(in.newAuth.buffer, in.newAuth.size);
57805788
}
57815789
if (rc == 0) {
57825790
rc = TPM2_HierarchyChangeAuth(&in);

wolftpm/tpm2.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3434,7 +3434,9 @@ WOLFTPM_API TPMI_ALG_HASH TPM2_GetTpmHashType(int hashType);
34343434
/*!
34353435
\ingroup TPM2_Proprietary
34363436
\brief Generate a fresh nonce of random numbers
3437-
\note Can use the TPM random number generator if WOLFTPM2_USE_HW_RNG is defined
3437+
\note Can use the TPM random number generator if WOLFTPM2_USE_HW_RNG is defined.
3438+
To force use of the TPM's RNG use WOLFTPM2_USE_HW_RNG. Please make sure you
3439+
have parameter encryption enabled to protect the RNG data over the bus.
34383440
34393441
\return TPM_RC_SUCCESS: successful
34403442
\return TPM_RC_FAILURE: generic failure (TPM IO issue or wolfcrypt configuration)
@@ -3456,6 +3458,9 @@ WOLFTPM_API TPMI_ALG_HASH TPM2_GetTpmHashType(int hashType);
34563458
*/
34573459
WOLFTPM_API int TPM2_GetNonce(byte* nonceBuf, int nonceSz);
34583460

3461+
/* Internal API for getting nonce without taking lock */
3462+
WOLFTPM_LOCAL int TPM2_GetNonceNoLock(byte* nonceBuf, int nonceSz);
3463+
34593464
/*!
34603465
\ingroup TPM2_Proprietary
34613466
\brief Helper function to prepare a correct PCR selection

0 commit comments

Comments
 (0)