Skip to content

Commit 02fcc54

Browse files
committed
Split TPM2_TransmitCommand by WOLFTPM_NO_RETRY and drop WithRetry name per review
1 parent 9652146 commit 02fcc54

1 file changed

Lines changed: 37 additions & 9 deletions

File tree

src/tpm2.c

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -467,23 +467,52 @@ static TPM_RC TPM2_SPDM_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
467467
}
468468
#endif /* WOLFTPM_SPDM */
469469

470+
#ifdef WOLFTPM_NO_RETRY
471+
/* Submit the finalized command in packet (length cmdSz) and parse the
472+
* response, returning the TPM response code. */
473+
static TPM_RC TPM2_TransmitCommand(TPM2_CTX* ctx, TPM2_Packet* packet,
474+
UINT32 cmdSz)
475+
{
476+
TPM_RC rc;
477+
478+
/* send command requires packet->pos to be the total command length */
479+
packet->pos = cmdSz;
480+
481+
#ifdef WOLFTPM_SPDM
482+
rc = TPM2_SPDM_SendCommand(ctx, packet);
483+
if (rc >= 0) {
484+
if (rc != TPM_RC_SUCCESS)
485+
return rc; /* SPDM active but failed, do not retry cleartext */
486+
}
487+
else /* rc < 0: SPDM not active, use normal transport */
488+
#endif
489+
{
490+
rc = (TPM_RC)INTERNAL_SEND_COMMAND(ctx, packet);
491+
}
492+
if (rc != 0)
493+
return rc; /* transport error */
494+
495+
/* parse response header and extract the TPM response code */
496+
rc = TPM2_Packet_Parse(rc, packet);
497+
498+
return rc;
499+
}
500+
#else
470501
/* Submit the finalized command in packet (length cmdSz) and parse the
471502
* response, returning the TPM response code. On TPM_RC_RETRY the TPM is
472503
* momentarily busy (e.g. persisting the daUsed flag on first auth use of a
473504
* non-noDA key) and asks for the identical command to be resubmitted; the
474505
* error response is header-only, so restoring the command header and resending
475506
* up to ctx->retries times recovers transparently. */
476-
static TPM_RC TPM2_TransmitWithRetry(TPM2_CTX* ctx, TPM2_Packet* packet,
507+
static TPM_RC TPM2_TransmitCommand(TPM2_CTX* ctx, TPM2_Packet* packet,
477508
UINT32 cmdSz)
478509
{
479510
TPM_RC rc;
480-
#ifndef WOLFTPM_NO_RETRY
481511
byte cmdHdr[TPM2_HEADER_SIZE];
482512
int origSize = packet->size;
483513
int retries = ctx->retries;
484514

485515
XMEMCPY(cmdHdr, packet->buf, TPM2_HEADER_SIZE);
486-
#endif
487516

488517
for (;;) {
489518
/* send command requires packet->pos to be the total command length */
@@ -506,20 +535,19 @@ static TPM_RC TPM2_TransmitWithRetry(TPM2_CTX* ctx, TPM2_Packet* packet,
506535
/* parse response header and extract the TPM response code */
507536
rc = TPM2_Packet_Parse(rc, packet);
508537

509-
#ifndef WOLFTPM_NO_RETRY
510538
if (TPM2_Packet_RetryRestore(rc, &retries, packet, cmdHdr, origSize)) {
511539
#ifdef DEBUG_WOLFTPM
512540
printf("TPM_RC_RETRY: resubmitting command, %d retries left\n",
513541
retries);
514542
#endif
515543
continue;
516544
}
517-
#endif
518545
break;
519546
}
520547

521548
return rc;
522549
}
550+
#endif /* WOLFTPM_NO_RETRY */
523551

524552
static TPM_RC TPM2_SendCommandAuth(TPM2_CTX* ctx, TPM2_Packet* packet,
525553
CmdInfo_t* info)
@@ -561,8 +589,8 @@ static TPM_RC TPM2_SendCommandAuth(TPM2_CTX* ctx, TPM2_Packet* packet,
561589
return rc;
562590
}
563591

564-
/* submit command and wait for response, auto-resubmitting on TPM_RC_RETRY */
565-
rc = TPM2_TransmitWithRetry(ctx, packet, cmdSz);
592+
/* submit command and parse the response */
593+
rc = TPM2_TransmitCommand(ctx, packet, cmdSz);
566594
respSz = packet->size;
567595

568596
/* restart the unmarshalling position */
@@ -595,8 +623,8 @@ static TPM_RC TPM2_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
595623
if (ctx == NULL || packet == NULL)
596624
return BAD_FUNC_ARG;
597625

598-
/* submit command and wait for response, auto-resubmitting on TPM_RC_RETRY */
599-
rc = TPM2_TransmitWithRetry(ctx, packet, (UINT32)packet->pos);
626+
/* submit command and parse the response */
627+
rc = TPM2_TransmitCommand(ctx, packet, (UINT32)packet->pos);
600628

601629
return rc;
602630
}

0 commit comments

Comments
 (0)