Skip to content

Commit 343c195

Browse files
authored
Merge pull request #468 from aidangarske/fix-fenrir-wolftpm-2
Fix fenrir issues
2 parents 95b0c15 + 2485b2d commit 343c195

9 files changed

Lines changed: 72 additions & 17 deletions

File tree

hal/tpm_io_espressif.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,21 @@
170170
static int _is_initialized_i2c = 0;
171171

172172
#ifdef DEBUG_WOLFSSL_VERBOSE
173+
/* Fixed buffer size for hex output (MAX_SPI_FRAMESIZE * 2 + 2) */
174+
#define SHOW_BINARY_HEX_BUF_SZ ((MAX_SPI_FRAMESIZE * 2) + 2)
173175
static esp_err_t show_binary(byte* theVar, size_t dataSz) {
174-
char hex_buffer[(dataSz * 2) + 2];
176+
char hex_buffer[SHOW_BINARY_HEX_BUF_SZ];
175177
word32 i;
178+
size_t maxSz;
179+
180+
/* Limit output to buffer capacity */
181+
maxSz = (dataSz > MAX_SPI_FRAMESIZE) ? MAX_SPI_FRAMESIZE : dataSz;
176182

177183
ESP_LOGI(TAG, "*********************************************************");
178-
for (i = 0; i < dataSz; i++) {
184+
for (i = 0; i < maxSz; i++) {
179185
snprintf(&hex_buffer[i * 2], 3, "%02X", (unsigned char)theVar[i]);
180186
}
187+
hex_buffer[maxSz * 2] = '\0';
181188
ESP_LOGI("TAG", "%s", hex_buffer);
182189
ESP_LOGI(TAG, "*********************************************************");
183190
return ESP_OK;
@@ -557,7 +564,13 @@ static int esp_spi_master_init(void)
557564
ret = spi_bus_add_device(SPI2_HOST, &dev_cfg, &spi);
558565
ESP_ERROR_CHECK(ret);
559566

560-
tpm_data = malloc(sizeof(struct TPM_DATA));
567+
tpm_data = (struct TPM_DATA*)XMALLOC(sizeof(struct TPM_DATA),
568+
NULL, DYNAMIC_TYPE_TMP_BUFFER);
569+
if (tpm_data == NULL) {
570+
spi_bus_remove_device(spi);
571+
spi_bus_free(SPI2_HOST);
572+
return TPM_RC_FAILURE;
573+
}
561574
tpm_data->spi = spi;
562575
tpm_data->cs_pin = PIN_NUM_CS;
563576
tpm_data->timeout_expiry = 0;

hal/tpm_io_microchip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ int TPM2_IoCb_Microchip_SPI(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
305305
/* Send Entire Message blocking - no wait states */
306306
if (DRV_SPI_WriteReadTransfer(handle, (byte*)txBuf, xferSz, rxBuf,
307307
xferSz) == true) {
308-
ret = TPM_RC_SUCCESS
308+
ret = TPM_RC_SUCCESS;
309309
}
310310

311311
(void)ctx;

hal/tpm_io_mmio.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,38 @@ static void TPM2_Mmio_Write8(word32 addr, byte *buf)
9090
sw_barrier();
9191
}
9292

93+
/* Maximum valid TPM register offset to prevent address wrap-around */
94+
#ifndef TPM_MMIO_MAX_OFFSET
95+
#define TPM_MMIO_MAX_OFFSET 0x1000000u /* 16MB - well above any valid TPM offset */
96+
#endif
97+
9398
int TPM2_IoCb_Mmio(TPM2_CTX *ctx, int isRead, word32 addr, byte* buf, word16 size,
9499
void* userCtx)
95100
{
96101
size_t i;
102+
word32 effectiveAddr;
103+
104+
/* Bounds check to prevent address wrap-around */
105+
if (addr >= TPM_MMIO_MAX_OFFSET) {
106+
return TPM_RC_FAILURE;
107+
}
108+
109+
effectiveAddr = MIMO_BASE_ADDRESS + addr;
97110

98111
/* IO for 32-bit aligned */
99112
for (i = 0; ((size_t)size - i) >= sizeof(word32); i += sizeof(word32)) {
100113
if (isRead)
101-
TPM2_Mmio_Read32(MIMO_BASE_ADDRESS + addr, buf + i);
114+
TPM2_Mmio_Read32(effectiveAddr, buf + i);
102115
else
103-
TPM2_Mmio_Write32(MIMO_BASE_ADDRESS + addr, buf + i);
116+
TPM2_Mmio_Write32(effectiveAddr, buf + i);
104117
}
105118

106119
/* IO for unaligned remainder */
107120
for (; i < (size_t)size; i++) {
108121
if (isRead)
109-
TPM2_Mmio_Read8(MIMO_BASE_ADDRESS + addr, buf + i);
122+
TPM2_Mmio_Read8(effectiveAddr, buf + i);
110123
else
111-
TPM2_Mmio_Write8(MIMO_BASE_ADDRESS + addr, buf + i);
124+
TPM2_Mmio_Write8(effectiveAddr, buf + i);
112125
}
113126

114127
(void)ctx;

hal/tpm_io_xilinx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
(byte*)txBuf, rxBuf, 1);
197197
if (status == XST_SUCCESS && rxBuf[0] & TPM_TIS_READY_MASK)
198198
break;
199-
} while (ret == TPM_RC_SUCCESS && --timeout > 0);
199+
} while (status == XST_SUCCESS && --timeout > 0);
200200
#ifdef WOLFTPM_DEBUG_TIMEOUT
201201
printf("SPI Ready Wait %d\n", TPM_SPI_WAIT_RETRY - timeout);
202202
#endif

src/tpm2.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,10 @@ static int TPM2_ResponseProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
377377
return rc;
378378
}
379379

380-
/* Verify HMAC */
380+
/* Verify HMAC using constant-time comparison */
381381
if (hmac.size != authRsp.hmac.size ||
382-
XMEMCMP(hmac.buffer, authRsp.hmac.buffer, hmac.size) != 0) {
382+
TPM2_ConstantCompare(hmac.buffer, authRsp.hmac.buffer,
383+
hmac.size) != 0) {
383384
#ifdef DEBUG_WOLFTPM
384385
printf("Response HMAC verification failed!\n");
385386
#endif
@@ -5537,7 +5538,7 @@ TPM_RC TPM2_GetProductInfo(uint8_t* info, uint16_t size)
55375538
if (payloadSz > (size_t)size) {
55385539
payloadSz = (size_t)size;
55395540
}
5540-
XMEMCPY(info, &packet.buf[25], payloadSz);
5541+
XMEMCPY(info, &packet.buf[26], payloadSz);
55415542
}
55425543
}
55435544
TPM2_ReleaseLock(ctx);
@@ -6620,6 +6621,17 @@ void TPM2_ForceZero(void* mem, word32 len)
66206621
while (len--) *z++ = 0;
66216622
}
66226623

6624+
/* Constant time memory comparison. Returns 0 if equal, non-zero if different. */
6625+
int TPM2_ConstantCompare(const byte* a, const byte* b, word32 len)
6626+
{
6627+
word32 i;
6628+
byte result = 0;
6629+
for (i = 0; i < len; i++) {
6630+
result |= a[i] ^ b[i];
6631+
}
6632+
return (int)result;
6633+
}
6634+
66236635
#ifdef DEBUG_WOLFTPM
66246636
#define LINE_LEN 16
66256637
void TPM2_PrintBin(const byte* buffer, word32 length)

src/tpm2_cryptocb.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,10 +647,12 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
647647

648648
/* clean hmac context */
649649
if (rc != 0 || info->hmac.digest != NULL) {
650-
wolfTPM2_UnloadHandle(tlsCtx->dev, &hmacCtx->hash.handle);
651-
wolfTPM2_UnloadHandle(tlsCtx->dev, &hmacCtx->key.handle);
652-
XFREE(hmacCtx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
653-
hmacCtx = NULL;
650+
if (hmacCtx != NULL) {
651+
wolfTPM2_UnloadHandle(tlsCtx->dev, &hmacCtx->hash.handle);
652+
wolfTPM2_UnloadHandle(tlsCtx->dev, &hmacCtx->key.handle);
653+
XFREE(hmacCtx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
654+
hmacCtx = NULL;
655+
}
654656
}
655657
info->hmac.hmac->devCtx = hmacCtx;
656658
#endif /* WOLFTPM_USE_SYMMETRIC */
@@ -739,11 +741,19 @@ static int wolfTPM2_HashUpdateCache(WOLFTPM2_HASHCTX* hashCtx,
739741
/* determine if we need to grow buffer */
740742
else if ((hashCtx->cacheSz + inSz) > hashCtx->cacheBufSz) {
741743
byte* oldIn = hashCtx->cacheBuf;
744+
word32 oldBufSz = hashCtx->cacheBufSz;
745+
/* check for overflow */
746+
if (hashCtx->cacheSz + inSz < hashCtx->cacheSz) {
747+
return BUFFER_E;
748+
}
742749
hashCtx->cacheBufSz = (hashCtx->cacheSz + inSz +
743750
WOLFTPM2_HASH_BLOCK_SZ - 1) & ~(WOLFTPM2_HASH_BLOCK_SZ - 1);
744-
hashCtx->cacheBuf = (byte*)XMALLOC(hashCtx->cacheBufSz,
751+
hashCtx->cacheBuf = (byte*)XMALLOC(hashCtx->cacheBufSz,
745752
NULL, DYNAMIC_TYPE_TMP_BUFFER);
746753
if (hashCtx->cacheBuf == NULL) {
754+
/* restore old buffer on allocation failure */
755+
hashCtx->cacheBuf = oldIn;
756+
hashCtx->cacheBufSz = oldBufSz;
747757
return MEMORY_E;
748758
}
749759
XMEMCPY(hashCtx->cacheBuf, oldIn, hashCtx->cacheSz);
@@ -919,6 +929,7 @@ static int RsaMGF1(wc_HashAlg* hash, enum wc_HashType hType,
919929
counter++;
920930
} while (ret == 0 && idx < outSz);
921931

932+
TPM2_ForceZero(tmp, sizeof(tmp));
922933
return ret;
923934
}
924935

@@ -1057,6 +1068,7 @@ static int RsaPadPss(const byte* input, word32 inputLen, byte* pkcsBlock,
10571068
xorbuf(m, salt + o, (word32)saltLen);
10581069
}
10591070
wc_HashFree(&hashCtx, hType);
1071+
TPM2_ForceZero(salt, sizeof(salt));
10601072
return ret;
10611073
}
10621074

src/tpm2_param_enc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ int TPM2_KDFa(
177177

178178
exit:
179179
wc_HmacFree(&hmac_ctx);
180+
TPM2_ForceZero(hash, sizeof(hash));
180181

181182
/* return length rounded up to nearest 8 multiple */
182183
return ret;

src/tpm2_swtpm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ static TPM_RC SwTpmConnect(TPM2_CTX* ctx, const char* host, const char* port)
191191
s = getaddrinfo(host, port, &hints, &result);
192192
if (s != 0) {
193193
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
194+
return rc;
194195
}
195196

196197
for (rp = result; rp != NULL; rp = rp->ai_next) {

wolftpm/tpm2.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3880,6 +3880,9 @@ WOLFTPM_API UINT16 TPM2_GetVendorID(void);
38803880
/* Internal helper API for ensuring memory is forcefully zero'd */
38813881
WOLFTPM_LOCAL void TPM2_ForceZero(void* mem, word32 len);
38823882

3883+
/* Constant time memory comparison */
3884+
WOLFTPM_LOCAL int TPM2_ConstantCompare(const byte* a, const byte* b, word32 len);
3885+
38833886

38843887
#ifdef DEBUG_WOLFTPM
38853888
/*!

0 commit comments

Comments
 (0)