Skip to content

Commit 423ce0d

Browse files
authored
Merge pull request #491 from aidangarske/fenrir-fixes-9
fwTPM/SPDM/src corrections and unit test additions
2 parents ba8dcee + c5145d8 commit 423ce0d

12 files changed

Lines changed: 452 additions & 89 deletions

File tree

examples/firmware/ifx_fw_extract.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
#define LOG(t) { printf(__FILE__":%i: %s\n", __LINE__, t); }
6464

6565
#define READ_BE16(dest, buf, size, off) { \
66-
if (off + sizeof(dest) >= size) { \
66+
if (off + sizeof(dest) > size) { \
6767
LOG("FW file too short"); \
6868
return -1; \
6969
} \
@@ -73,7 +73,7 @@
7373
}
7474

7575
#define READ_BE32(dest, buf, size, off) { \
76-
if (off + sizeof(dest) >= size) { \
76+
if (off + sizeof(dest) > size) { \
7777
LOG("FW file too short"); \
7878
return -1; \
7979
} \
@@ -172,7 +172,7 @@ static int extractFW(
172172
}
173173

174174
READ_BE32(size32, fw, fw_size, offset);
175-
if (offset + size32 >= fw_size) {
175+
if (offset + size32 > fw_size) {
176176
LOG("FW file too short");
177177
return -1;
178178
}

examples/firmware/ifx_fw_update.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ int TPM2_IFX_Firmware_Update(void* userCtx, int argc, char *argv[])
170170
else {
171171
printf("Success: Please reset or power cycle TPM\n");
172172
}
173-
return rc;
173+
goto exit;
174174
}
175175

176176
if (manifest_file == NULL || firmware_file == NULL) {

examples/firmware/st33_fw_update.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ int TPM2_ST33_Firmware_Update(void* userCtx, int argc, char *argv[])
264264
else {
265265
printf("Success: Please reset or power cycle TPM\n");
266266
}
267-
return rc;
267+
goto exit;
268268
}
269269

270270
if (fi_file == NULL) {

examples/pcr/quote.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,17 @@ int TPM2_PCR_Quote_Test(void* userCtx, int argc, char *argv[])
9898

9999
/* Advanced usage */
100100
if (argv[1][0] != '-') {
101-
if (pcrIndex < 0 || pcrIndex > 23 || *argv[1] < '0' || *argv[1] > '9') {
101+
if (*argv[1] < '0' || *argv[1] > '9') {
102102
printf("PCR index is out of range (0-23)\n");
103103
usage();
104104
return 0;
105105
}
106106
pcrIndex = XATOI(argv[1]);
107+
if (pcrIndex < 0 || pcrIndex > 23) {
108+
printf("PCR index is out of range (0-23)\n");
109+
usage();
110+
return 0;
111+
}
107112
}
108113
if (argc >= 3 && argv[2][0] != '-')
109114
outputFile = argv[2];

examples/pkcs7/pkcs7.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,14 @@ static int PKCS7_SignVerifyEx(WOLFTPM2_DEV* dev, int tpmDevId,
115115

116116
XMEMSET(&pkcs7, 0, sizeof(pkcs7));
117117

118-
hashSz = wc_HashGetDigestSize(hashType);
119-
if (hashSz <= 0) {
120-
return hashSz;
118+
rc = wc_HashGetDigestSize(hashType);
119+
if (rc <= 0) {
120+
/* Preserve the wolfCrypt error on negatives; for a 0 return
121+
* (not currently produced by wolfCrypt), report BAD_FUNC_ARG
122+
* rather than masquerading as success. */
123+
return (rc < 0) ? rc : BAD_FUNC_ARG;
121124
}
125+
hashSz = (word32)rc;
122126

123127
/* calculate hash for content */
124128
rc = wc_HashInit(&hash, hashType);

hal/tpm_io_infineon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
#include <Qspi/SpiMaster/IfxQspi_SpiMaster.h>
187187

188188
/* externally declared SPI master channel */
189-
extern IfxQspi_SpiMaster_Channel spiMasterChannel
189+
extern IfxQspi_SpiMaster_Channel spiMasterChannel;
190190

191191
static int TPM2_IoCb_Infineon_TriCore_SPI(TPM2_CTX* ctx, const byte* txBuf,
192192
byte* rxBuf, word16 xferSz, void* userCtx)

hal/tpm_io_microchip.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@
223223
return -1;
224224
}
225225

226+
busy_retry = TPM_I2C_TRIES;
227+
226228
while (I2C_BB_IsBusy() && --busy_retry > 0) {
227229
microchip_wait(250);
228230
}

src/fwtpm/fwtpm_command.c

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,46 @@ static void FwLookupEntityAuth(FWTPM_CTX* ctx, TPM_HANDLE handle,
429429
}
430430
}
431431

432+
/* Constant-time password vs authValue comparison.
433+
* Iterates a fixed upper bound (TPM_MAX_DIGEST_SIZE) with bitwise masks so
434+
* neither the trip count nor per-iteration work depends on the secret
435+
* authValSz. Trailing zeros on either side are treated as insignificant
436+
* (matches TCG reference for authValues padded to nameAlg digest size).
437+
* Returns 1 on mismatch, 0 on match. Precondition: pwSz and avSz must
438+
* each be <= TPM_MAX_DIGEST_SIZE; out-of-range inputs fail closed. */
439+
static int FwCtAuthCompare(const byte* password, int pwSz,
440+
const byte* authVal, int avSz)
441+
{
442+
byte zeroAuth[TPM_MAX_DIGEST_SIZE];
443+
const byte* avPtr;
444+
volatile byte diff = 0;
445+
int ci;
446+
447+
if (pwSz < 0 || avSz < 0 ||
448+
pwSz > TPM_MAX_DIGEST_SIZE || avSz > TPM_MAX_DIGEST_SIZE) {
449+
return 1;
450+
}
451+
452+
XMEMSET(zeroAuth, 0, sizeof(zeroAuth));
453+
avPtr = (authVal != NULL) ? authVal : zeroAuth;
454+
455+
for (ci = 0; ci < TPM_MAX_DIGEST_SIZE; ci++) {
456+
/* 0xFF if ci < bound, else 0x00. Use UINT32 (guaranteed 32-bit
457+
* wolfTPM typedef) so the >> 31 shift is always well-defined. */
458+
byte pwMask = (byte)-((UINT32)(ci - pwSz) >> 31);
459+
byte avMask = (byte)-((UINT32)(ci - avSz) >> 31);
460+
byte overlap = (byte)(pwMask & avMask);
461+
/* Overlap region: bytes must match */
462+
diff |= (byte)((password[ci] ^ avPtr[ci]) & overlap);
463+
/* Trailing bytes of pw past avSz must be zero */
464+
diff |= (byte)(password[ci] & (pwMask & (byte)~avMask));
465+
/* Trailing bytes of av past pwSz must be zero */
466+
diff |= (byte)(avPtr[ci] & (avMask & (byte)~pwMask));
467+
}
468+
469+
return ((int)diff != 0) ? 1 : 0;
470+
}
471+
432472
/* Compute cpHash = H(commandCode || name1 || ... || cpBuffer)
433473
* Per TPM 2.0 Part 1 Section 18.7 */
434474
static int FwComputeCpHash(TPMI_ALG_HASH hashAlg, TPM_CC cmdCode,
@@ -9975,6 +10015,7 @@ static TPM_RC FwCmd_NV_DefineSpace(FWTPM_CTX* ctx, TPM2_Packet* cmd,
997510015
FwRspNoParams(rsp, cmdTag);
997610016
}
997710017

10018+
TPM2_ForceZero(&auth, sizeof(auth));
997810019
return rc;
997910020
}
998010021

@@ -12719,9 +12760,7 @@ int FWTPM_ProcessCommand(FWTPM_CTX* ctx,
1271912760
int doEncRsp = 0; /* Encrypt outgoing response param */
1272012761
#endif
1272112762
int pj, hj; /* Loop indices for auth validation */
12722-
int pwSz, avSz, maxSz, minSz, authFail; /* Password comparison */
12723-
volatile byte diff;
12724-
int ci;
12763+
int authFail; /* Password comparison result */
1272512764

1272612765
if (ctx == NULL || cmdBuf == NULL || rspBuf == NULL || rspSize == NULL) {
1272712766
return BAD_FUNC_ARG;
@@ -13085,29 +13124,8 @@ int FWTPM_ProcessCommand(FWTPM_CTX* ctx,
1308513124

1308613125
FwLookupEntityAuth(ctx, entityH, &authVal, &authValSz);
1308713126

13088-
/* Compare password with authValue in constant time.
13089-
* Per TCG reference implementation, trailing zeros are
13090-
* insignificant (handles authValues padded to nameAlg
13091-
* digest size). We compare up to the max of both sizes
13092-
* and verify trailing bytes are zero, all in constant
13093-
* time to avoid leaking the effective auth length. */
13094-
diff = 0;
13095-
pwSz = (int)cmdAuths[pj].passwordSize;
13096-
avSz = authValSz;
13097-
maxSz = (pwSz > avSz) ? pwSz : avSz;
13098-
minSz = (pwSz < avSz) ? pwSz : avSz;
13099-
/* Compare overlapping portion */
13100-
for (ci = 0; ci < minSz; ci++) {
13101-
diff |= cmdAuths[pj].password[ci] ^ authVal[ci];
13102-
}
13103-
/* Verify trailing bytes of the longer buffer are zero */
13104-
for (ci = minSz; ci < maxSz; ci++) {
13105-
if (ci < pwSz)
13106-
diff |= cmdAuths[pj].password[ci];
13107-
if (ci < avSz)
13108-
diff |= authVal[ci];
13109-
}
13110-
authFail = ((int)diff != 0);
13127+
authFail = FwCtAuthCompare(cmdAuths[pj].password,
13128+
(int)cmdAuths[pj].passwordSize, authVal, authValSz);
1311113129
if (authFail) {
1311213130
#ifdef DEBUG_WOLFTPM
1311313131
printf("fwTPM: Password auth failed for handle "

0 commit comments

Comments
 (0)