Skip to content

Commit f1f6337

Browse files
committed
Remove uneeded braces
1 parent c8dcf5b commit f1f6337

5 files changed

Lines changed: 124 additions & 135 deletions

File tree

src/spdm/spdm_msg.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ int wolfSPDM_BuildFinish(WOLFSPDM_CTX* ctx, byte* buf, word32* bufSz)
201201
byte signature[WOLFSPDM_ECC_POINT_SIZE]; /* 96 bytes for P-384 */
202202
word32 sigSz = sizeof(signature);
203203
word32 offset = 4; /* Start after header */
204+
word32 minSz;
204205
int mutualAuth = 0;
205206
int rc;
206207

@@ -220,15 +221,13 @@ int wolfSPDM_BuildFinish(WOLFSPDM_CTX* ctx, byte* buf, word32* bufSz)
220221

221222
/* Check buffer size: header(4) + [OpaqueLength(2) for 1.4+] +
222223
* [signature(96) for mutual auth] + HMAC(48) */
223-
{
224-
word32 minSz = 4 + WOLFSPDM_HASH_SIZE; /* header + HMAC */
225-
if (ctx->spdmVersion >= SPDM_VERSION_14)
226-
minSz += 2; /* OpaqueLength */
227-
if (mutualAuth)
228-
minSz += WOLFSPDM_ECC_POINT_SIZE; /* Signature */
229-
if (*bufSz < minSz)
230-
return WOLFSPDM_E_BUFFER_SMALL;
231-
}
224+
minSz = 4 + WOLFSPDM_HASH_SIZE; /* header + HMAC */
225+
if (ctx->spdmVersion >= SPDM_VERSION_14)
226+
minSz += 2; /* OpaqueLength */
227+
if (mutualAuth)
228+
minSz += WOLFSPDM_ECC_POINT_SIZE; /* Signature */
229+
if (*bufSz < minSz)
230+
return WOLFSPDM_E_BUFFER_SMALL;
232231

233232
/* Build FINISH header */
234233
buf[0] = ctx->spdmVersion;
@@ -352,6 +351,7 @@ int wolfSPDM_ParseVersion(WOLFSPDM_CTX* ctx, const byte* buf, word32 bufSz)
352351
word16 maxEntries;
353352
word32 i;
354353
byte highestVersion = 0; /* No version found yet */
354+
byte maxVer;
355355

356356
SPDM_CHECK_PARSE_ARGS(ctx, buf, bufSz, 6);
357357
SPDM_CHECK_RESPONSE(ctx, buf, bufSz, SPDM_VERSION, WOLFSPDM_E_VERSION_MISMATCH);
@@ -372,17 +372,15 @@ int wolfSPDM_ParseVersion(WOLFSPDM_CTX* ctx, const byte* buf, word32 bufSz)
372372
* Per DSP0274, negotiated version must be the highest version
373373
* that both sides support. We support WOLFSPDM_MIN_SPDM_VERSION
374374
* through WOLFSPDM_MAX_SPDM_VERSION (or ctx->maxVersion if set). */
375-
{
376-
byte maxVer = (ctx->maxVersion != 0) ? ctx->maxVersion
377-
: WOLFSPDM_MAX_SPDM_VERSION;
378-
for (i = 0; i < entryCount; i++) {
379-
/* Each entry is 2 bytes; high byte (offset +1) is Major.Minor */
380-
byte ver = buf[6 + i * 2 + 1];
381-
if (ver >= WOLFSPDM_MIN_SPDM_VERSION &&
382-
ver <= maxVer &&
383-
ver > highestVersion) {
384-
highestVersion = ver;
385-
}
375+
maxVer = (ctx->maxVersion != 0) ? ctx->maxVersion
376+
: WOLFSPDM_MAX_SPDM_VERSION;
377+
for (i = 0; i < entryCount; i++) {
378+
/* Each entry is 2 bytes; high byte (offset +1) is Major.Minor */
379+
byte ver = buf[6 + i * 2 + 1];
380+
if (ver >= WOLFSPDM_MIN_SPDM_VERSION &&
381+
ver <= maxVer &&
382+
ver > highestVersion) {
383+
highestVersion = ver;
386384
}
387385
}
388386

src/spdm/spdm_psk.c

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,16 @@ int wolfSPDM_DeriveHandshakeKeysPsk(WOLFSPDM_CTX* ctx, const byte* th1Hash)
322322
int wolfSPDM_ConnectPsk(WOLFSPDM_CTX* ctx)
323323
{
324324
int rc;
325+
byte txBuf[128];
326+
byte rxBuf[WOLFSPDM_MAX_MSG_SIZE + WOLFSPDM_AEAD_OVERHEAD];
327+
byte finBuf[64];
328+
byte encBuf[WOLFSPDM_MAX_MSG_SIZE + WOLFSPDM_AEAD_OVERHEAD];
329+
byte decBuf[64];
330+
word32 txSz;
331+
word32 rxSz;
332+
word32 finSz;
333+
word32 encSz;
334+
word32 decSz;
325335

326336
if (ctx == NULL) {
327337
return WOLFSPDM_E_INVALID_ARG;
@@ -354,72 +364,62 @@ int wolfSPDM_ConnectPsk(WOLFSPDM_CTX* ctx)
354364
* NS350 supports direct GET_VERSION -> PSK_EXCHANGE. */
355365

356366
/* Step 2: PSK_EXCHANGE / PSK_EXCHANGE_RSP */
357-
{
358-
byte txBuf[128];
359-
byte rxBuf[WOLFSPDM_VENDOR_RX_SZ];
360-
word32 txSz = sizeof(txBuf);
361-
word32 rxSz = sizeof(rxBuf);
362-
363-
wolfSPDM_DebugPrint(ctx, "PSK Step 4: PSK_EXCHANGE\n");
364-
rc = wolfSPDM_BuildPskExchange(ctx, txBuf, &txSz);
365-
if (rc != WOLFSPDM_SUCCESS) {
366-
ctx->state = WOLFSPDM_STATE_ERROR;
367-
return rc;
368-
}
369-
rc = wolfSPDM_TranscriptAdd(ctx, txBuf, txSz);
370-
if (rc != WOLFSPDM_SUCCESS) {
371-
ctx->state = WOLFSPDM_STATE_ERROR;
372-
return rc;
373-
}
374-
rc = wolfSPDM_SendReceive(ctx, txBuf, txSz, rxBuf, &rxSz);
375-
if (rc != WOLFSPDM_SUCCESS) {
376-
ctx->state = WOLFSPDM_STATE_ERROR;
377-
return rc;
378-
}
379-
rc = wolfSPDM_ParsePskExchangeRsp(ctx, rxBuf, rxSz);
380-
if (rc != WOLFSPDM_SUCCESS) {
381-
ctx->state = WOLFSPDM_STATE_ERROR;
382-
return rc;
383-
}
367+
txSz = sizeof(txBuf);
368+
rxSz = sizeof(rxBuf);
369+
370+
wolfSPDM_DebugPrint(ctx, "PSK Step 4: PSK_EXCHANGE\n");
371+
rc = wolfSPDM_BuildPskExchange(ctx, txBuf, &txSz);
372+
if (rc != WOLFSPDM_SUCCESS) {
373+
ctx->state = WOLFSPDM_STATE_ERROR;
374+
return rc;
375+
}
376+
rc = wolfSPDM_TranscriptAdd(ctx, txBuf, txSz);
377+
if (rc != WOLFSPDM_SUCCESS) {
378+
ctx->state = WOLFSPDM_STATE_ERROR;
379+
return rc;
380+
}
381+
rc = wolfSPDM_SendReceive(ctx, txBuf, txSz, rxBuf, &rxSz);
382+
if (rc != WOLFSPDM_SUCCESS) {
383+
ctx->state = WOLFSPDM_STATE_ERROR;
384+
return rc;
385+
}
386+
rc = wolfSPDM_ParsePskExchangeRsp(ctx, rxBuf, rxSz);
387+
if (rc != WOLFSPDM_SUCCESS) {
388+
ctx->state = WOLFSPDM_STATE_ERROR;
389+
return rc;
384390
}
385391

386392
/* Step 5: PSK_FINISH / PSK_FINISH_RSP (encrypted) */
387-
{
388-
byte finBuf[64];
389-
byte encBuf[WOLFSPDM_MAX_MSG_SIZE + WOLFSPDM_AEAD_OVERHEAD];
390-
byte rxBuf[WOLFSPDM_MAX_MSG_SIZE + WOLFSPDM_AEAD_OVERHEAD];
391-
byte decBuf[64];
392-
word32 finSz = sizeof(finBuf);
393-
word32 encSz = sizeof(encBuf);
394-
word32 rxSz = sizeof(rxBuf);
395-
word32 decSz = sizeof(decBuf);
396-
397-
wolfSPDM_DebugPrint(ctx, "PSK Step 5: PSK_FINISH\n");
398-
rc = wolfSPDM_BuildPskFinish(ctx, finBuf, &finSz);
399-
if (rc != WOLFSPDM_SUCCESS) {
400-
ctx->state = WOLFSPDM_STATE_ERROR;
401-
return rc;
402-
}
403-
rc = wolfSPDM_EncryptInternal(ctx, finBuf, finSz, encBuf, &encSz);
404-
if (rc != WOLFSPDM_SUCCESS) {
405-
ctx->state = WOLFSPDM_STATE_ERROR;
406-
return rc;
407-
}
408-
rc = wolfSPDM_SendReceive(ctx, encBuf, encSz, rxBuf, &rxSz);
409-
if (rc != WOLFSPDM_SUCCESS) {
410-
ctx->state = WOLFSPDM_STATE_ERROR;
411-
return rc;
412-
}
413-
rc = wolfSPDM_DecryptInternal(ctx, rxBuf, rxSz, decBuf, &decSz);
414-
if (rc != WOLFSPDM_SUCCESS) {
415-
ctx->state = WOLFSPDM_STATE_ERROR;
416-
return rc;
417-
}
418-
rc = wolfSPDM_ParsePskFinishRsp(ctx, decBuf, decSz);
419-
if (rc != WOLFSPDM_SUCCESS) {
420-
ctx->state = WOLFSPDM_STATE_ERROR;
421-
return rc;
422-
}
393+
finSz = sizeof(finBuf);
394+
encSz = sizeof(encBuf);
395+
rxSz = sizeof(rxBuf);
396+
decSz = sizeof(decBuf);
397+
398+
wolfSPDM_DebugPrint(ctx, "PSK Step 5: PSK_FINISH\n");
399+
rc = wolfSPDM_BuildPskFinish(ctx, finBuf, &finSz);
400+
if (rc != WOLFSPDM_SUCCESS) {
401+
ctx->state = WOLFSPDM_STATE_ERROR;
402+
return rc;
403+
}
404+
rc = wolfSPDM_EncryptInternal(ctx, finBuf, finSz, encBuf, &encSz);
405+
if (rc != WOLFSPDM_SUCCESS) {
406+
ctx->state = WOLFSPDM_STATE_ERROR;
407+
return rc;
408+
}
409+
rc = wolfSPDM_SendReceive(ctx, encBuf, encSz, rxBuf, &rxSz);
410+
if (rc != WOLFSPDM_SUCCESS) {
411+
ctx->state = WOLFSPDM_STATE_ERROR;
412+
return rc;
413+
}
414+
rc = wolfSPDM_DecryptInternal(ctx, rxBuf, rxSz, decBuf, &decSz);
415+
if (rc != WOLFSPDM_SUCCESS) {
416+
ctx->state = WOLFSPDM_STATE_ERROR;
417+
return rc;
418+
}
419+
rc = wolfSPDM_ParsePskFinishRsp(ctx, decBuf, decSz);
420+
if (rc != WOLFSPDM_SUCCESS) {
421+
ctx->state = WOLFSPDM_STATE_ERROR;
422+
return rc;
423423
}
424424

425425
/* Derive application data keys */

src/spdm/spdm_tcg.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ int wolfSPDM_TCG_VendorCmdClear(WOLFSPDM_CTX* ctx, const char* vdCode,
4343
byte rxBuf[WOLFSPDM_VENDOR_RX_SZ];
4444
word32 rxSz;
4545
int rc;
46+
byte ver;
4647

47-
{
48-
byte ver = ctx->spdmVersion ? ctx->spdmVersion : SPDM_VERSION_13;
49-
spdmMsgSz = wolfSPDM_BuildVendorDefined(ver, vdCode, payload,
50-
payloadSz, spdmMsg, sizeof(spdmMsg));
51-
}
48+
ver = ctx->spdmVersion ? ctx->spdmVersion : SPDM_VERSION_13;
49+
spdmMsgSz = wolfSPDM_BuildVendorDefined(ver, vdCode, payload,
50+
payloadSz, spdmMsg, sizeof(spdmMsg));
5251
if (spdmMsgSz < 0) {
5352
return spdmMsgSz;
5453
}
@@ -92,12 +91,11 @@ int wolfSPDM_TCG_VendorCmdSecured(WOLFSPDM_CTX* ctx, const char* vdCode,
9291
byte decBuf[WOLFSPDM_VENDOR_BUF_SZ];
9392
word32 decSz;
9493
int rc;
94+
byte ver;
9595

96-
{
97-
byte ver = ctx->spdmVersion ? ctx->spdmVersion : SPDM_VERSION_13;
98-
spdmMsgSz = wolfSPDM_BuildVendorDefined(ver, vdCode, payload,
99-
payloadSz, spdmMsg, sizeof(spdmMsg));
100-
}
96+
ver = ctx->spdmVersion ? ctx->spdmVersion : SPDM_VERSION_13;
97+
spdmMsgSz = wolfSPDM_BuildVendorDefined(ver, vdCode, payload,
98+
payloadSz, spdmMsg, sizeof(spdmMsg));
10199
if (spdmMsgSz < 0) {
102100
return spdmMsgSz;
103101
}

src/spdm/unit_test.c

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ static int test_invalid_curve_point(void)
545545
{
546546
byte badX[WOLFSPDM_ECC_KEY_SIZE];
547547
byte badY[WOLFSPDM_ECC_KEY_SIZE];
548+
byte zeros[WOLFSPDM_ECC_KEY_SIZE];
548549
int rc;
549550
TEST_CTX_SETUP_V12();
550551

@@ -562,13 +563,10 @@ static int test_invalid_curve_point(void)
562563
ASSERT_EQ(rc, WOLFSPDM_E_CRYPTO_FAIL, "Off-curve point must be rejected");
563564

564565
/* Verify shared secret was zeroed on failure */
565-
{
566-
byte zeros[WOLFSPDM_ECC_KEY_SIZE];
567-
memset(zeros, 0, sizeof(zeros));
568-
ASSERT_EQ(memcmp(ctx->sharedSecret, zeros, sizeof(ctx->sharedSecret)), 0,
569-
"sharedSecret must be zeroed on failure");
570-
ASSERT_EQ(ctx->sharedSecretSz, 0, "sharedSecretSz must be 0 on failure");
571-
}
566+
memset(zeros, 0, sizeof(zeros));
567+
ASSERT_EQ(memcmp(ctx->sharedSecret, zeros, sizeof(ctx->sharedSecret)), 0,
568+
"sharedSecret must be zeroed on failure");
569+
ASSERT_EQ(ctx->sharedSecretSz, 0, "sharedSecretSz must be 0 on failure");
572570

573571
TEST_CTX_FREE();
574572
TEST_PASS();
@@ -1083,17 +1081,17 @@ static int test_parse_tcg_clear_message(void)
10831081
byte buf[32], payload[16];
10841082
word32 payloadSz = sizeof(payload);
10851083
WOLFSPDM_TCG_CLEAR_HDR hdr;
1084+
int built;
1085+
int parsed;
10861086
TEST_CTX_SETUP();
10871087
printf("test_parse_tcg_clear_message...\n");
10881088

10891089
/* Build a valid message first */
10901090
ctx->connectionHandle = 0;
10911091
ctx->fipsIndicator = 0;
1092-
{
1093-
int built = wolfSPDM_BuildTcgClearMessage(ctx, (byte*)"ABCD", 4, buf,
1094-
sizeof(buf));
1095-
TEST_ASSERT(built == 20, "build failed");
1096-
}
1092+
built = wolfSPDM_BuildTcgClearMessage(ctx, (byte*)"ABCD", 4, buf,
1093+
sizeof(buf));
1094+
TEST_ASSERT(built == 20, "build failed");
10971095

10981096
/* NULL args */
10991097
TEST_ASSERT(wolfSPDM_ParseTcgClearMessage(NULL, 20, payload, &payloadSz,
@@ -1107,13 +1105,11 @@ static int test_parse_tcg_clear_message(void)
11071105

11081106
/* Valid parse (returns payload size on success) */
11091107
payloadSz = sizeof(payload);
1110-
{
1111-
int parsed = wolfSPDM_ParseTcgClearMessage(buf, 20, payload,
1112-
&payloadSz, &hdr);
1113-
TEST_ASSERT(parsed >= 0, "parse failed");
1114-
ASSERT_EQ(payloadSz, 4, "payload size wrong");
1115-
TEST_ASSERT(memcmp(payload, "ABCD", 4) == 0, "payload mismatch");
1116-
}
1108+
parsed = wolfSPDM_ParseTcgClearMessage(buf, 20, payload,
1109+
&payloadSz, &hdr);
1110+
TEST_ASSERT(parsed >= 0, "parse failed");
1111+
ASSERT_EQ(payloadSz, 4, "payload size wrong");
1112+
TEST_ASSERT(memcmp(payload, "ABCD", 4) == 0, "payload mismatch");
11171113

11181114
TEST_CTX_FREE();
11191115
TEST_PASS();
@@ -1157,6 +1153,7 @@ static int test_parse_vendor_defined(void)
11571153
char vdCode[9];
11581154
word32 payloadSz;
11591155
int built;
1156+
int parsed;
11601157
printf("test_parse_vendor_defined...\n");
11611158

11621159
/* Build, then parse back */
@@ -1165,14 +1162,12 @@ static int test_parse_vendor_defined(void)
11651162
TEST_ASSERT(built > 0, "build failed");
11661163

11671164
payloadSz = sizeof(payload);
1168-
{
1169-
int parsed = wolfSPDM_ParseVendorDefined(outBuf, (word32)built, vdCode,
1170-
payload, &payloadSz);
1171-
TEST_ASSERT(parsed >= 0, "parse failed");
1172-
TEST_ASSERT(memcmp(vdCode, "TPM2_CMD", 8) == 0, "vdCode mismatch");
1173-
ASSERT_EQ(payloadSz, 5, "payload size wrong");
1174-
TEST_ASSERT(memcmp(payload, "HELLO", 5) == 0, "payload mismatch");
1175-
}
1165+
parsed = wolfSPDM_ParseVendorDefined(outBuf, (word32)built, vdCode,
1166+
payload, &payloadSz);
1167+
TEST_ASSERT(parsed >= 0, "parse failed");
1168+
TEST_ASSERT(memcmp(vdCode, "TPM2_CMD", 8) == 0, "vdCode mismatch");
1169+
ASSERT_EQ(payloadSz, 5, "payload size wrong");
1170+
TEST_ASSERT(memcmp(payload, "HELLO", 5) == 0, "payload mismatch");
11761171

11771172
/* NULL args */
11781173
TEST_ASSERT(wolfSPDM_ParseVendorDefined(NULL, (word32)built, vdCode,
@@ -1194,6 +1189,7 @@ static int test_vendor_defined_roundtrip(void)
11941189
char vdCode[9];
11951190
word32 payloadSz;
11961191
int i, built;
1192+
int parsed;
11971193
printf("test_vendor_defined_roundtrip...\n");
11981194

11991195
for (i = 0; i < 5; i++) {
@@ -1202,14 +1198,12 @@ static int test_vendor_defined_roundtrip(void)
12021198
testData, 4, outBuf, sizeof(outBuf));
12031199
TEST_ASSERT(built > 0, "build failed");
12041200
payloadSz = sizeof(payload);
1205-
{
1206-
int parsed = wolfSPDM_ParseVendorDefined(outBuf, (word32)built,
1207-
vdCode, payload, &payloadSz);
1208-
TEST_ASSERT(parsed >= 0, "parse failed");
1209-
TEST_ASSERT(memcmp(vdCode, codes[i], 8) == 0, "vdCode mismatch");
1210-
ASSERT_EQ(payloadSz, 4, "payload size");
1211-
TEST_ASSERT(memcmp(payload, testData, 4) == 0, "payload mismatch");
1212-
}
1201+
parsed = wolfSPDM_ParseVendorDefined(outBuf, (word32)built,
1202+
vdCode, payload, &payloadSz);
1203+
TEST_ASSERT(parsed >= 0, "parse failed");
1204+
TEST_ASSERT(memcmp(vdCode, codes[i], 8) == 0, "vdCode mismatch");
1205+
ASSERT_EQ(payloadSz, 4, "payload size");
1206+
TEST_ASSERT(memcmp(payload, testData, 4) == 0, "payload mismatch");
12131207
}
12141208

12151209
TEST_PASS();

src/tpm2_spdm.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,14 @@ int wolfTPM2_SPDM_SecuredExchange(
219219
char rspVdCode[WOLFSPDM_VDCODE_LEN + 1];
220220
int vdMsgSz;
221221
int rc;
222+
byte ver;
222223

223224
/* Wrap TPM command in SPDM VENDOR_DEFINED_REQUEST("TPM2_CMD") */
224-
{
225-
byte ver = wolfSPDM_GetNegotiatedVersion(ctx->spdmCtx);
226-
if (ver == 0) ver = SPDM_VERSION_13;
227-
vdMsgSz = wolfSPDM_BuildVendorDefined(ver,
228-
WOLFSPDM_VDCODE_TPM2_CMD,
229-
cmdPlain, cmdSz, vdMsg, sizeof(vdMsg));
230-
}
225+
ver = wolfSPDM_GetNegotiatedVersion(ctx->spdmCtx);
226+
if (ver == 0) ver = SPDM_VERSION_13;
227+
vdMsgSz = wolfSPDM_BuildVendorDefined(ver,
228+
WOLFSPDM_VDCODE_TPM2_CMD,
229+
cmdPlain, cmdSz, vdMsg, sizeof(vdMsg));
231230
if (vdMsgSz < 0) {
232231
return vdMsgSz;
233232
}

0 commit comments

Comments
 (0)