|
37 | 37 | /* --- Local Variables -- */ |
38 | 38 | /******************************************************************************/ |
39 | 39 |
|
40 | | - |
41 | | -#ifdef WOLFTPM_NO_ACTIVE_THREAD_LS |
42 | | -/* if using gHwLock and want to use a shared active TPM2_CTX between threads */ |
43 | | -static TPM2_CTX* gActiveTPM; |
44 | | -#else |
45 | | -static THREAD_LS_T TPM2_CTX* gActiveTPM; |
46 | | -#endif |
47 | | - |
48 | 40 | #ifndef WOLFTPM2_NO_WOLFCRYPT |
49 | 41 | static volatile int gWolfCryptRefCount = 0; |
50 | 42 | #endif |
51 | 43 |
|
52 | 44 | #if !defined(WOLFTPM2_NO_WOLFCRYPT) && !defined(WOLFTPM_NO_LOCK) && \ |
53 | 45 | !defined(SINGLE_THREADED) |
| 46 | +/* if a mutex lock is supported, then don't use thread local on gActiveTPM */ |
| 47 | +#undef WOLFTPM_NO_ACTIVE_THREAD_LS |
| 48 | +#define WOLFTPM_NO_ACTIVE_THREAD_LS |
| 49 | + |
54 | 50 | static wolfSSL_Mutex gHwLock WOLFSSL_MUTEX_INITIALIZER_CLAUSE(gHwLock); |
55 | 51 | #endif |
56 | 52 |
|
| 53 | +#ifdef WOLFTPM_NO_ACTIVE_THREAD_LS |
| 54 | +/* if using gHwLock and want to use a shared active TPM2_CTX between threads */ |
| 55 | +static TPM2_CTX* gActiveTPM; |
| 56 | +#else |
| 57 | +static THREAD_LS_T TPM2_CTX* gActiveTPM; |
| 58 | +#endif |
| 59 | + |
57 | 60 | #ifdef WOLFTPM_LINUX_DEV |
58 | 61 | #define INTERNAL_SEND_COMMAND TPM2_LINUX_SendCommand |
59 | 62 | #define TPM2_INTERNAL_CLEANUP(ctx) |
@@ -160,7 +163,7 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet, |
160 | 163 |
|
161 | 164 | if (session->sessionHandle != TPM_RS_PW) { |
162 | 165 | /* Generate fresh nonce */ |
163 | | - rc = TPM2_GetNonce(session->nonceCaller.buffer, |
| 166 | + rc = TPM2_GetNonceNoLock(session->nonceCaller.buffer, |
164 | 167 | session->nonceCaller.size); |
165 | 168 | if (rc != TPM_RC_SUCCESS) { |
166 | 169 | return rc; |
@@ -5464,6 +5467,7 @@ TPM_RC TPM2_GetProductInfo(uint8_t* info, uint16_t size) |
5464 | 5467 | size = packet.size - 26; |
5465 | 5468 | XMEMCPY(info, &packet.buf[25], size); |
5466 | 5469 | } |
| 5470 | + TPM2_ReleaseLock(ctx); |
5467 | 5471 | } |
5468 | 5472 | return rc; |
5469 | 5473 | } |
@@ -5686,9 +5690,7 @@ int TPM2_GetHashType(TPMI_ALG_HASH hashAlg) |
5686 | 5690 | return 0; |
5687 | 5691 | } |
5688 | 5692 |
|
5689 | | -/* Can optionally define WOLFTPM2_USE_HW_RNG to force using TPM hardware for |
5690 | | - * RNG source */ |
5691 | | -int TPM2_GetNonce(byte* nonceBuf, int nonceSz) |
| 5693 | +int TPM2_GetNonceNoLock(byte* nonceBuf, int nonceSz) |
5692 | 5694 | { |
5693 | 5695 | int rc; |
5694 | 5696 | TPM2_CTX* ctx = TPM2_GetActiveCtx(); |
@@ -5717,44 +5719,58 @@ int TPM2_GetNonce(byte* nonceBuf, int nonceSz) |
5717 | 5719 | #else |
5718 | 5720 | /* Call GetRandom directly, so a custom packet buffer can be used. |
5719 | 5721 | * This won't conflict when being called from TPM2_CommandProcess. */ |
5720 | | - rc = TPM2_AcquireLock(ctx); |
5721 | | - if (rc == TPM_RC_SUCCESS) { |
5722 | | - while (randSz < nonceSz) { |
5723 | | - UINT16 inSz = nonceSz - randSz, outSz = 0; |
5724 | | - if (inSz > MAX_RNG_REQ_SIZE) { |
5725 | | - inSz = MAX_RNG_REQ_SIZE; |
5726 | | - } |
| 5722 | + while (randSz < nonceSz) { |
| 5723 | + UINT16 inSz = nonceSz - randSz, outSz = 0; |
| 5724 | + if (inSz > MAX_RNG_REQ_SIZE) { |
| 5725 | + inSz = MAX_RNG_REQ_SIZE; |
| 5726 | + } |
5727 | 5727 |
|
5728 | | - TPM2_Packet_InitBuf(&packet, buffer, (int)sizeof(buffer)); |
5729 | | - TPM2_Packet_AppendU16(&packet, inSz); |
5730 | | - TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_GetRandom); |
5731 | | - rc = TPM2_SendCommand(ctx, &packet); |
5732 | | - #ifdef WOLFTPM_DEBUG_VERBOSE |
5733 | | - printf("TPM2_GetNonce (%d bytes at %d): %d (%s)\n", |
5734 | | - inSz, randSz, rc, TPM2_GetRCString(rc)); |
5735 | | - #endif |
5736 | | - if (rc != TPM_RC_SUCCESS) { |
5737 | | - break; |
5738 | | - } |
| 5728 | + TPM2_Packet_InitBuf(&packet, buffer, (int)sizeof(buffer)); |
| 5729 | + TPM2_Packet_AppendU16(&packet, inSz); |
| 5730 | + TPM2_Packet_Finalize(&packet, TPM_ST_NO_SESSIONS, TPM_CC_GetRandom); |
| 5731 | + rc = TPM2_SendCommand(ctx, &packet); |
| 5732 | + #ifdef WOLFTPM_DEBUG_VERBOSE |
| 5733 | + printf("TPM2_GetNonce (%d bytes at %d): %d (%s)\n", |
| 5734 | + inSz, randSz, rc, TPM2_GetRCString(rc)); |
| 5735 | + #endif |
| 5736 | + if (rc != TPM_RC_SUCCESS) { |
| 5737 | + break; |
| 5738 | + } |
5739 | 5739 |
|
5740 | | - TPM2_Packet_ParseU16(&packet, &outSz); |
5741 | | - if (outSz > MAX_RNG_REQ_SIZE) { |
5742 | | - #ifdef DEBUG_WOLFTPM |
5743 | | - printf("TPM2_GetNonce out size error\n"); |
5744 | | - #endif |
5745 | | - rc = BAD_FUNC_ARG; |
5746 | | - break; |
5747 | | - } |
5748 | | - TPM2_Packet_ParseBytes(&packet, &nonceBuf[randSz], outSz); |
5749 | | - randSz += outSz; |
| 5740 | + TPM2_Packet_ParseU16(&packet, &outSz); |
| 5741 | + if (outSz > MAX_RNG_REQ_SIZE) { |
| 5742 | + #ifdef DEBUG_WOLFTPM |
| 5743 | + printf("TPM2_GetNonce out size error\n"); |
| 5744 | + #endif |
| 5745 | + rc = BAD_FUNC_ARG; |
| 5746 | + break; |
5750 | 5747 | } |
5751 | | - TPM2_ReleaseLock(ctx); |
| 5748 | + TPM2_Packet_ParseBytes(&packet, &nonceBuf[randSz], outSz); |
| 5749 | + randSz += outSz; |
5752 | 5750 | } |
5753 | 5751 | #endif |
5754 | 5752 |
|
5755 | 5753 | return rc; |
5756 | 5754 | } |
5757 | 5755 |
|
| 5756 | +int TPM2_GetNonce(byte* nonceBuf, int nonceSz) |
| 5757 | +{ |
| 5758 | + int rc; |
| 5759 | + TPM2_CTX* ctx = TPM2_GetActiveCtx(); |
| 5760 | + |
| 5761 | + if (ctx == NULL) { |
| 5762 | + return BAD_FUNC_ARG; |
| 5763 | + } |
| 5764 | + |
| 5765 | + rc = TPM2_AcquireLock(ctx); |
| 5766 | + if (rc == TPM_RC_SUCCESS) { |
| 5767 | + rc = TPM2_GetNonceNoLock(nonceBuf, nonceSz); |
| 5768 | + TPM2_ReleaseLock(ctx); |
| 5769 | + } |
| 5770 | + |
| 5771 | + return rc; |
| 5772 | +} |
| 5773 | + |
5758 | 5774 | /* Get name for object/handle */ |
5759 | 5775 | int TPM2_GetName(TPM2_CTX* ctx, UINT32 handleValue, int handleCnt, int idx, TPM2B_NAME* name) |
5760 | 5776 | { |
|
0 commit comments