Skip to content

Commit b684e06

Browse files
committed
Estimated savings: ~350-400 lines removed, ~28 bytes per WOLFSPDM_CTX from bit-field packing.
1 parent d6482f2 commit b684e06

7 files changed

Lines changed: 336 additions & 437 deletions

File tree

spdm/src/spdm_context.c

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ int wolfSPDM_Init(WOLFSPDM_CTX* ctx)
4444
if (rc != 0) {
4545
return WOLFSPDM_E_CRYPTO_FAIL;
4646
}
47-
ctx->rngInitialized = 1;
47+
ctx->flags.rngInitialized = 1;
4848

4949
/* Set default requester capabilities */
5050
ctx->reqCaps = WOLFSPDM_DEFAULT_REQ_CAPS;
5151

5252
/* Set default session ID (0x0001 is valid; 0x0000/0xFFFF are reserved) */
5353
ctx->reqSessionId = 0x0001;
5454

55-
ctx->initialized = 1;
55+
ctx->flags.initialized = 1;
5656
/* isDynamic remains 0 — only wolfSPDM_New sets it */
5757

5858
return WOLFSPDM_SUCCESS;
@@ -73,7 +73,7 @@ WOLFSPDM_CTX* wolfSPDM_New(void)
7373
XFREE(ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
7474
return NULL;
7575
}
76-
ctx->isDynamic = 1; /* Tag AFTER Init so it isn't wiped */
76+
ctx->flags.isDynamic = 1; /* Tag AFTER Init so it isn't wiped */
7777

7878
return ctx;
7979
}
@@ -87,29 +87,29 @@ void wolfSPDM_Free(WOLFSPDM_CTX* ctx)
8787

8888
#ifdef WOLFSPDM_DYNAMIC_MEMORY
8989
{
90-
int wasDynamic = ctx->isDynamic;
90+
int wasDynamic = ctx->flags.isDynamic;
9191
#endif
9292

9393
/* Free RNG */
94-
if (ctx->rngInitialized) {
94+
if (ctx->flags.rngInitialized) {
9595
wc_FreeRng(&ctx->rng);
9696
}
9797

9898
/* Free ephemeral key */
99-
if (ctx->ephemeralKeyInitialized) {
99+
if (ctx->flags.ephemeralKeyInit) {
100100
wc_ecc_free(&ctx->ephemeralKey);
101101
}
102102

103103
/* Free responder public key (used for measurement/challenge verification) */
104-
if (ctx->hasResponderPubKey) {
104+
if (ctx->flags.hasResponderPubKey) {
105105
wc_ecc_free(&ctx->responderPubKey);
106106
}
107107

108108
#ifndef NO_WOLFSPDM_CHALLENGE
109109
/* Free M1/M2 challenge hash if still initialized */
110-
if (ctx->m1m2HashInit) {
110+
if (ctx->flags.m1m2HashInit) {
111111
wc_Sha384Free(&ctx->m1m2Hash);
112-
ctx->m1m2HashInit = 0;
112+
ctx->flags.m1m2HashInit = 0;
113113
}
114114
#endif
115115

@@ -169,7 +169,7 @@ int wolfSPDM_SetResponderPubKey(WOLFSPDM_CTX* ctx,
169169

170170
XMEMCPY(ctx->rspPubKey, pubKey, pubKeySz);
171171
ctx->rspPubKeyLen = pubKeySz;
172-
ctx->hasRspPubKey = 1;
172+
ctx->flags.hasRspPubKey = 1;
173173

174174
return WOLFSPDM_SUCCESS;
175175
}
@@ -191,7 +191,7 @@ int wolfSPDM_SetRequesterKeyPair(WOLFSPDM_CTX* ctx,
191191
ctx->reqPrivKeyLen = privKeySz;
192192
XMEMCPY(ctx->reqPubKey, pubKey, pubKeySz);
193193
ctx->reqPubKeyLen = pubKeySz;
194-
ctx->hasReqKeyPair = 1;
194+
ctx->flags.hasReqKeyPair = 1;
195195

196196
return WOLFSPDM_SUCCESS;
197197
}
@@ -225,15 +225,15 @@ int wolfSPDM_SetTrustedCAs(WOLFSPDM_CTX* ctx, const byte* derCerts,
225225

226226
XMEMCPY(ctx->trustedCAs, derCerts, derCertsSz);
227227
ctx->trustedCAsSz = derCertsSz;
228-
ctx->hasTrustedCAs = 1;
228+
ctx->flags.hasTrustedCAs = 1;
229229

230230
return WOLFSPDM_SUCCESS;
231231
}
232232

233233
void wolfSPDM_SetDebug(WOLFSPDM_CTX* ctx, int enable)
234234
{
235235
if (ctx != NULL) {
236-
ctx->debug = enable;
236+
ctx->flags.debug = enable;
237237
}
238238
}
239239

@@ -334,11 +334,11 @@ static int wolfSPDM_ConnectStandard(WOLFSPDM_CTX* ctx)
334334
wolfSPDM_GetCertificate(ctx, 0));
335335

336336
/* Validate certificate chain if trusted CAs are loaded */
337-
if (ctx->hasTrustedCAs) {
337+
if (ctx->flags.hasTrustedCAs) {
338338
SPDM_CONNECT_STEP(ctx, "Validating certificate chain\n",
339339
wolfSPDM_ValidateCertChain(ctx));
340340
}
341-
else if (!ctx->hasResponderPubKey) {
341+
else if (!ctx->flags.hasResponderPubKey) {
342342
wolfSPDM_DebugPrint(ctx,
343343
"Warning: No trusted CAs loaded — chain not validated\n");
344344
}
@@ -361,7 +361,7 @@ int wolfSPDM_Connect(WOLFSPDM_CTX* ctx)
361361
return WOLFSPDM_E_INVALID_ARG;
362362
}
363363

364-
if (!ctx->initialized) {
364+
if (!ctx->flags.initialized) {
365365
return WOLFSPDM_E_BAD_STATE;
366366
}
367367

@@ -453,11 +453,8 @@ int wolfSPDM_SendReceive(WOLFSPDM_CTX* ctx,
453453
if (totalSz > sizeof(tcgTx)) {
454454
return WOLFSPDM_E_BUFFER_SMALL;
455455
}
456-
SPDM_Set16BE(tcgTx, WOLFSPDM_TCG_TAG_SECURED);
457-
SPDM_Set32BE(tcgTx + 2, totalSz);
458-
SPDM_Set32BE(tcgTx + 6, ctx->connectionHandle);
459-
SPDM_Set16BE(tcgTx + 10, ctx->fipsIndicator);
460-
XMEMSET(tcgTx + 12, 0, 4); /* Reserved */
456+
wolfSPDM_WriteTcgHeader(tcgTx, WOLFSPDM_TCG_TAG_SECURED,
457+
totalSz, ctx->connectionHandle, ctx->fipsIndicator);
461458
XMEMCPY(tcgTx + WOLFSPDM_TCG_HEADER_SIZE, txBuf, txSz);
462459
tcgTxSz = (int)totalSz;
463460
}
@@ -538,7 +535,7 @@ void wolfSPDM_DebugPrint(WOLFSPDM_CTX* ctx, const char* fmt, ...)
538535
{
539536
va_list args;
540537

541-
if (ctx == NULL || !ctx->debug) {
538+
if (ctx == NULL || !ctx->flags.debug) {
542539
return;
543540
}
544541

@@ -554,7 +551,7 @@ void wolfSPDM_DebugHex(WOLFSPDM_CTX* ctx, const char* label,
554551
{
555552
word32 i;
556553

557-
if (ctx == NULL || !ctx->debug || data == NULL) {
554+
if (ctx == NULL || !ctx->flags.debug || data == NULL) {
558555
return;
559556
}
560557

@@ -575,7 +572,7 @@ void wolfSPDM_DebugHex(WOLFSPDM_CTX* ctx, const char* label,
575572

576573
int wolfSPDM_GetMeasurementCount(WOLFSPDM_CTX* ctx)
577574
{
578-
if (ctx == NULL || !ctx->hasMeasurements) {
575+
if (ctx == NULL || !ctx->flags.hasMeasurements) {
579576
return 0;
580577
}
581578
return (int)ctx->measBlockCount;
@@ -586,7 +583,7 @@ int wolfSPDM_GetMeasurementBlock(WOLFSPDM_CTX* ctx, int blockIdx,
586583
{
587584
const WOLFSPDM_MEAS_BLOCK* blk;
588585

589-
if (ctx == NULL || !ctx->hasMeasurements) {
586+
if (ctx == NULL || !ctx->flags.hasMeasurements) {
590587
return WOLFSPDM_E_INVALID_ARG;
591588
}
592589
if (blockIdx < 0 || blockIdx >= (int)ctx->measBlockCount) {

spdm/src/spdm_crypto.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ int wolfSPDM_GetRandom(WOLFSPDM_CTX* ctx, byte* out, word32 outSz)
4242
return WOLFSPDM_E_INVALID_ARG;
4343
}
4444

45-
if (!ctx->rngInitialized) {
45+
if (!ctx->flags.rngInitialized) {
4646
return WOLFSPDM_E_BAD_STATE;
4747
}
4848

@@ -64,14 +64,14 @@ int wolfSPDM_GenerateEphemeralKey(WOLFSPDM_CTX* ctx)
6464
return WOLFSPDM_E_INVALID_ARG;
6565
}
6666

67-
if (!ctx->rngInitialized) {
67+
if (!ctx->flags.rngInitialized) {
6868
return WOLFSPDM_E_BAD_STATE;
6969
}
7070

7171
/* Free existing key if any */
72-
if (ctx->ephemeralKeyInitialized) {
72+
if (ctx->flags.ephemeralKeyInit) {
7373
wc_ecc_free(&ctx->ephemeralKey);
74-
ctx->ephemeralKeyInitialized = 0;
74+
ctx->flags.ephemeralKeyInit = 0;
7575
}
7676

7777
/* Initialize new key */
@@ -87,7 +87,7 @@ int wolfSPDM_GenerateEphemeralKey(WOLFSPDM_CTX* ctx)
8787
return WOLFSPDM_E_CRYPTO_FAIL;
8888
}
8989

90-
ctx->ephemeralKeyInitialized = 1;
90+
ctx->flags.ephemeralKeyInit = 1;
9191
wolfSPDM_DebugPrint(ctx, "Generated P-384 ephemeral key\n");
9292

9393
return WOLFSPDM_SUCCESS;
@@ -104,7 +104,7 @@ int wolfSPDM_ExportEphemeralPubKey(WOLFSPDM_CTX* ctx,
104104
return WOLFSPDM_E_INVALID_ARG;
105105
}
106106

107-
if (!ctx->ephemeralKeyInitialized) {
107+
if (!ctx->flags.ephemeralKeyInit) {
108108
return WOLFSPDM_E_BAD_STATE;
109109
}
110110

@@ -135,7 +135,7 @@ int wolfSPDM_ComputeSharedSecret(WOLFSPDM_CTX* ctx,
135135
return WOLFSPDM_E_INVALID_ARG;
136136
}
137137

138-
if (!ctx->ephemeralKeyInitialized) {
138+
if (!ctx->flags.ephemeralKeyInit) {
139139
return WOLFSPDM_E_BAD_STATE;
140140
}
141141

@@ -199,7 +199,7 @@ int wolfSPDM_SignHash(WOLFSPDM_CTX* ctx, const byte* hash, word32 hashSz,
199199
return WOLFSPDM_E_INVALID_ARG;
200200
}
201201

202-
if (!ctx->hasReqKeyPair || ctx->reqPrivKeyLen == 0) {
202+
if (!ctx->flags.hasReqKeyPair || ctx->reqPrivKeyLen == 0) {
203203
wolfSPDM_DebugPrint(ctx, "No requester key pair for signing\n");
204204
return WOLFSPDM_E_BAD_STATE;
205205
}

spdm/src/spdm_internal.h

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,20 @@ struct WOLFSPDM_CTX {
8585
/* State machine */
8686
int state;
8787

88-
/* Configuration flags */
89-
int debug;
90-
int initialized;
91-
int isDynamic; /* Set by wolfSPDM_New(), checked by wolfSPDM_Free() */
88+
/* Boolean flags — packed into a bit-field struct to save ~28 bytes */
89+
struct {
90+
byte debug : 1;
91+
byte initialized : 1;
92+
byte isDynamic : 1; /* Set by wolfSPDM_New(), checked by Free */
93+
byte rngInitialized : 1;
94+
byte ephemeralKeyInit : 1;
95+
byte hasRspPubKey : 1;
96+
byte hasReqKeyPair : 1;
97+
byte hasMeasurements : 1;
98+
byte hasResponderPubKey : 1;
99+
byte hasTrustedCAs : 1;
100+
byte m1m2HashInit : 1;
101+
} flags;
92102

93103
/* Protocol mode (standard SPDM or Nuvoton) */
94104
WOLFSPDM_MODE mode;
@@ -109,7 +119,6 @@ struct WOLFSPDM_CTX {
109119

110120
/* Random number generator */
111121
WC_RNG rng;
112-
int rngInitialized;
113122

114123
/* Negotiated parameters */
115124
byte spdmVersion; /* Negotiated SPDM version */
@@ -120,7 +129,6 @@ struct WOLFSPDM_CTX {
120129

121130
/* Ephemeral ECDHE key (generated for KEY_EXCHANGE) */
122131
ecc_key ephemeralKey;
123-
int ephemeralKeyInitialized;
124132

125133
/* ECDH shared secret (P-384 X-coordinate = 48 bytes) */
126134
byte sharedSecret[WOLFSPDM_ECC_KEY_SIZE];
@@ -165,14 +173,12 @@ struct WOLFSPDM_CTX {
165173
/* Responder's identity public key (for cert-less mode like Nuvoton) */
166174
byte rspPubKey[128]; /* TPMT_PUBLIC (120 bytes for P-384) or raw X||Y (96) */
167175
word32 rspPubKeyLen;
168-
int hasRspPubKey;
169176

170177
/* Requester's identity key pair (for mutual auth) */
171178
byte reqPrivKey[WOLFSPDM_ECC_KEY_SIZE];
172179
word32 reqPrivKeyLen;
173180
byte reqPubKey[WOLFSPDM_ECC_POINT_SIZE];
174181
word32 reqPubKeyLen;
175-
int hasReqKeyPair;
176182

177183
/* Message buffers */
178184
byte sendBuf[WOLFSPDM_MAX_MSG_SIZE + WOLFSPDM_AEAD_TAG_SIZE];
@@ -186,7 +192,6 @@ struct WOLFSPDM_CTX {
186192
byte measSummaryHash[WOLFSPDM_HASH_SIZE]; /* Summary hash from response */
187193
byte measSignature[WOLFSPDM_ECC_SIG_SIZE]; /* Captured signature (96 bytes P-384) */
188194
word32 measSignatureSize; /* 0 if unsigned, 96 if signed */
189-
int hasMeasurements;
190195

191196
#ifndef NO_WOLFSPDM_MEAS_VERIFY
192197
/* Saved GET_MEASUREMENTS request for L1/L2 transcript */
@@ -197,12 +202,10 @@ struct WOLFSPDM_CTX {
197202

198203
/* Responder identity for signature verification (measurements + challenge) */
199204
ecc_key responderPubKey; /* Extracted from cert chain leaf */
200-
int hasResponderPubKey; /* 1 if key extracted successfully */
201205

202206
/* Certificate chain validation */
203207
byte trustedCAs[WOLFSPDM_MAX_CERT_CHAIN]; /* DER-encoded root CAs */
204208
word32 trustedCAsSz;
205-
int hasTrustedCAs; /* 1 if CAs loaded */
206209

207210
#ifndef NO_WOLFSPDM_CHALLENGE
208211
/* Challenge authentication */
@@ -217,7 +220,6 @@ struct WOLFSPDM_CTX {
217220
* This hash accumulates A+B during NegAlgo/GetDigests/GetCertificate,
218221
* then C is added in VerifyChallengeAuthSig. */
219222
wc_Sha384 m1m2Hash;
220-
int m1m2HashInit; /* 1 if m1m2Hash is initialized */
221223
#endif
222224

223225
/* Key update state — app secrets for re-derivation */
@@ -268,6 +270,20 @@ static WC_INLINE word64 SPDM_Get64LE(const byte* buf) {
268270
((word64)buf[6] << 48) | ((word64)buf[7] << 56);
269271
}
270272

273+
/* Write TCG SPDM Binding header (16 bytes): tag(2/BE) + size(4/BE) +
274+
* connHandle(4/BE) + fips(2/BE) + reserved(4) */
275+
#ifdef WOLFSPDM_NUVOTON
276+
static WC_INLINE void wolfSPDM_WriteTcgHeader(byte* buf, word16 tag,
277+
word32 totalSz, word32 connHandle, word16 fips)
278+
{
279+
SPDM_Set16BE(buf, tag);
280+
SPDM_Set32BE(buf + 2, totalSz);
281+
SPDM_Set32BE(buf + 6, connHandle);
282+
SPDM_Set16BE(buf + 10, fips);
283+
XMEMSET(buf + 12, 0, 4); /* Reserved */
284+
}
285+
#endif
286+
271287
/* Build IV: BaseIV XOR zero-extended sequence number (DSP0277) */
272288
static WC_INLINE void wolfSPDM_BuildIV(byte* iv, const byte* baseIv,
273289
word64 seqNum, int nuvotonMode)

0 commit comments

Comments
 (0)