Skip to content

Commit 5a2fee0

Browse files
committed
File Reorginization and review feedback
- Moved spdm/src/*.c src/spdm/*.c (11 source files) - Moved spdm/wolfspdm/*.h wolftpm/spdm/*.h (7 public headers) - Moved spdm/src/spdm_internal.h src/spdm/spdm_internal.h - Moved spdm/test/unit_test.c src/spdm/unit_test.c - Moved spdm/README.md src/spdm/README.md - Deleted spdm/include.am, created src/spdm/include.am - Renamed all #include <wolfspdm/...> #include <wolftpm/spdm/...> across all files - Added SPDM headers to wolftpm/include.am (as nobase_include_HEADERS) - Updated Makefile.am: include spdm/include.am include src/spdm/include.am - Removed -I/spdm from configure.ac and examples/spdm/include.am (no longer needed
1 parent 49c7674 commit 5a2fee0

25 files changed

Lines changed: 203 additions & 139 deletions

.github/workflows/make-test-swtpm.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ jobs:
8080
wolfssl_config: --enable-wolftpm --enable-ecc --enable-sha384 --enable-aesgcm --enable-hkdf --enable-sp
8181
wolftpm_config: --enable-spdm --enable-nuvoton
8282
needs_swtpm: false
83-
# SPDM dynamic memory
84-
- name: spdm-dynamic-mem
83+
# SPDM small stack (heap-allocated SPDM context)
84+
- name: spdm-smallstack
8585
wolfssl_config: --enable-wolftpm --enable-ecc --enable-sha384 --enable-aesgcm --enable-hkdf --enable-sp
86-
wolftpm_config: --enable-spdm --enable-nuvoton --enable-spdm-dynamic-mem
86+
wolftpm_config: --enable-spdm --enable-nuvoton --enable-smallstack
8787
needs_swtpm: false
8888
# SPDM debug
8989
- name: spdm-debug
@@ -100,10 +100,10 @@ jobs:
100100
wolfssl_config: --enable-wolftpm --enable-ecc --enable-sha384 --enable-aesgcm --enable-hkdf --enable-sp
101101
wolftpm_config: --enable-spdm --enable-nations --enable-debug
102102
needs_swtpm: false
103-
# SPDM + Nations dynamic memory
104-
- name: spdm-nations-dynamic-mem
103+
# SPDM + Nations small stack (heap-allocated SPDM context)
104+
- name: spdm-nations-smallstack
105105
wolfssl_config: --enable-wolftpm --enable-ecc --enable-sha384 --enable-aesgcm --enable-hkdf --enable-sp
106-
wolftpm_config: --enable-spdm --enable-nations --enable-spdm-dynamic-mem
106+
wolftpm_config: --enable-spdm --enable-nations --enable-smallstack
107107
needs_swtpm: false
108108
# Microchip
109109
- name: microchip

configure.ac

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,6 @@ AC_ARG_WITH([wolfspdm],
486486
[AS_HELP_STRING([--with-wolfspdm=PATH],[DEPRECATED: Use --enable-spdm instead.])],
487487
[AC_MSG_ERROR([--with-wolfspdm is no longer needed. Use --enable-spdm instead.])])
488488

489-
# SPDM dynamic memory (default: static/zero-malloc)
490-
AC_ARG_ENABLE([spdm-dynamic-mem],
491-
[AS_HELP_STRING([--enable-spdm-dynamic-mem],[SPDM: Use heap allocation for context (default: static)])],
492-
[ ENABLED_SPDM_DYNMEM=$enableval ],
493-
[ ENABLED_SPDM_DYNMEM=no ]
494-
)
495-
496489
if test "x$ENABLED_SPDM" = "xyes"
497490
then
498491
AC_DEFINE([WOLFTPM_SPDM], [1], [Enable SPDM support])
@@ -511,11 +504,6 @@ then
511504
AC_MSG_NOTICE([Nations Technology SPDM vendor commands enabled])
512505
fi
513506

514-
if test "x$ENABLED_SPDM_DYNMEM" = "xyes"
515-
then
516-
AC_DEFINE([WOLFSPDM_DYNAMIC_MEMORY], [1], [SPDM: Enable dynamic memory allocation])
517-
fi
518-
519507
if test "x$ax_enable_debug" != "xno"
520508
then
521509
AC_DEFINE([WOLFSPDM_DEBUG], [1], [SPDM: Enable debug output])
@@ -689,6 +677,3 @@ echo " * Nations Tech NS350: $ENABLED_NATIONS"
689677
echo " * Runtime Module Detection: $ENABLED_AUTODETECT"
690678
echo " * Firmware Upgrade Support: $ENABLED_FIRMWARE"
691679
echo " * SPDM Support: $ENABLED_SPDM"
692-
if test "x$ENABLED_SPDM" = "xyes"; then
693-
echo " * SPDM Dynamic Mem: $ENABLED_SPDM_DYNMEM"
694-
fi

src/spdm/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ make
184184
| `--enable-nuvoton` | Enable Nuvoton TPM hardware support |
185185
| `--enable-nations` | Enable Nations NS350 hardware support |
186186
| `--enable-debug` | Debug output with verbose SPDM tracing |
187-
| `--enable-spdm-dynamic-mem` | Heap-allocated SPDM context (default: static ~32 KB) |
187+
| `--enable-smallstack` | Heap-allocated SPDM context (default: static ~32 KB) |
188188

189189
## Usage
190190

@@ -418,7 +418,7 @@ intercepted and routed through SPDM when a session is active.
418418
**Static (default):** Zero heap allocation. SPDM context uses ~32 KB of
419419
static memory, ideal for embedded environments.
420420

421-
**Dynamic (`--enable-spdm-dynamic-mem`):** Context is heap-allocated.
421+
**Small stack (`--enable-smallstack`):** Context is heap-allocated.
422422
Useful on platforms with small stacks.
423423

424424
## wolfSPDM API

src/spdm/spdm_context.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22+
#ifdef HAVE_CONFIG_H
23+
#include <config.h>
24+
#endif
25+
26+
#ifdef WOLFTPM_SPDM
27+
2228
#include "spdm_internal.h"
2329
#include <stdarg.h>
2430
#include <stdio.h>
@@ -53,7 +59,7 @@ int wolfSPDM_Init(WOLFSPDM_CTX* ctx)
5359
return WOLFSPDM_SUCCESS;
5460
}
5561

56-
#ifdef WOLFSPDM_DYNAMIC_MEMORY
62+
#ifdef WOLFTPM_SMALL_STACK
5763
WOLFSPDM_CTX* wolfSPDM_New(void)
5864
{
5965
WOLFSPDM_CTX* ctx;
@@ -72,18 +78,17 @@ WOLFSPDM_CTX* wolfSPDM_New(void)
7278

7379
return ctx;
7480
}
75-
#endif /* WOLFSPDM_DYNAMIC_MEMORY */
81+
#endif /* WOLFTPM_SMALL_STACK */
7682

7783
void wolfSPDM_Free(WOLFSPDM_CTX* ctx)
7884
{
85+
int wasDynamic;
86+
7987
if (ctx == NULL) {
8088
return;
8189
}
8290

83-
#ifdef WOLFSPDM_DYNAMIC_MEMORY
84-
{
85-
int wasDynamic = ctx->flags.isDynamic;
86-
#endif
91+
wasDynamic = ctx->flags.isDynamic;
8792

8893
/* Free RNG */
8994
if (ctx->flags.rngInitialized) {
@@ -98,11 +103,12 @@ void wolfSPDM_Free(WOLFSPDM_CTX* ctx)
98103
/* Zero entire struct (covers all sensitive key material) */
99104
wc_ForceZero(ctx, sizeof(WOLFSPDM_CTX));
100105

101-
#ifdef WOLFSPDM_DYNAMIC_MEMORY
102-
if (wasDynamic) {
103-
XFREE(ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
104-
}
106+
#ifdef WOLFTPM_SMALL_STACK
107+
if (wasDynamic) {
108+
XFREE(ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
105109
}
110+
#else
111+
(void)wasDynamic;
106112
#endif
107113
}
108114

@@ -547,3 +553,5 @@ const char* wolfSPDM_GetErrorString(int error)
547553
default: return "Unknown error";
548554
}
549555
}
556+
557+
#endif /* WOLFTPM_SPDM */

src/spdm/spdm_crypto.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22+
#ifdef HAVE_CONFIG_H
23+
#include <config.h>
24+
#endif
25+
26+
#ifdef WOLFTPM_SPDM
27+
2228
#include "spdm_internal.h"
2329

2430
/* Left-pad a buffer in-place to targetSz with leading zeros */
@@ -346,3 +352,5 @@ int wolfSPDM_SignHash(WOLFSPDM_CTX* ctx, const byte* hash, word32 hashSz,
346352

347353
return (rc == 0) ? WOLFSPDM_SUCCESS : WOLFSPDM_E_CRYPTO_FAIL;
348354
}
355+
356+
#endif /* WOLFTPM_SPDM */

src/spdm/spdm_internal.h

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ struct WOLFSPDM_CTX {
7272
/* State machine */
7373
int state;
7474

75-
/* Boolean flag bit field */
76-
struct {
77-
unsigned int debug : 1;
78-
unsigned int initialized : 1;
79-
unsigned int isDynamic : 1; /* Set by wolfSPDM_New(), checked by Free */
80-
unsigned int rngInitialized : 1;
81-
unsigned int ephemeralKeyInit : 1;
82-
unsigned int hasRspPubKey : 1;
83-
unsigned int hasReqKeyPair : 1;
84-
} flags;
85-
8675
/* Protocol mode */
8776
WOLFSPDM_MODE mode;
8877

@@ -166,6 +155,16 @@ struct WOLFSPDM_CTX {
166155
word32 reqPrivKeyLen;
167156
byte reqPubKey[WOLFSPDM_ECC_POINT_SIZE];
168157

158+
/* Boolean flag bit field (at end for better struct packing) */
159+
struct {
160+
unsigned int debug : 1;
161+
unsigned int initialized : 1;
162+
unsigned int isDynamic : 1; /* Set by wolfSPDM_New(), checked by Free */
163+
unsigned int rngInitialized : 1;
164+
unsigned int ephemeralKeyInit : 1;
165+
unsigned int hasRspPubKey : 1;
166+
unsigned int hasReqKeyPair : 1;
167+
} flags;
169168
};
170169

171170
/* ----- Byte-Order Helpers ----- */
@@ -276,78 +275,78 @@ static WC_INLINE void wolfSPDM_BuildIV(byte* iv, const byte* baseIv,
276275

277276
/* ----- Internal Function Declarations - Transcript ----- */
278277

279-
WOLFSPDM_API void wolfSPDM_TranscriptReset(WOLFSPDM_CTX* ctx);
280-
WOLFSPDM_API int wolfSPDM_TranscriptAdd(WOLFSPDM_CTX* ctx, const byte* data, word32 len);
281-
WOLFSPDM_API int wolfSPDM_TranscriptHash(WOLFSPDM_CTX* ctx, byte* hash);
282-
WOLFSPDM_API int wolfSPDM_Sha384Hash(byte* out,
278+
WOLFTPM_LOCAL void wolfSPDM_TranscriptReset(WOLFSPDM_CTX* ctx);
279+
WOLFTPM_LOCAL int wolfSPDM_TranscriptAdd(WOLFSPDM_CTX* ctx, const byte* data, word32 len);
280+
WOLFTPM_LOCAL int wolfSPDM_TranscriptHash(WOLFSPDM_CTX* ctx, byte* hash);
281+
WOLFTPM_LOCAL int wolfSPDM_Sha384Hash(byte* out,
283282
const byte* d1, word32 d1Sz,
284283
const byte* d2, word32 d2Sz,
285284
const byte* d3, word32 d3Sz);
286285

287286
/* ----- Internal Function Declarations - Crypto ----- */
288287

289-
WOLFSPDM_API int wolfSPDM_GenerateEphemeralKey(WOLFSPDM_CTX* ctx);
290-
WOLFSPDM_API int wolfSPDM_ExportEphemeralPubKey(WOLFSPDM_CTX* ctx,
288+
WOLFTPM_LOCAL int wolfSPDM_GenerateEphemeralKey(WOLFSPDM_CTX* ctx);
289+
WOLFTPM_LOCAL int wolfSPDM_ExportEphemeralPubKey(WOLFSPDM_CTX* ctx,
291290
byte* pubKeyX, word32* pubKeyXSz,
292291
byte* pubKeyY, word32* pubKeyYSz);
293-
WOLFSPDM_API int wolfSPDM_ComputeSharedSecret(WOLFSPDM_CTX* ctx,
292+
WOLFTPM_LOCAL int wolfSPDM_ComputeSharedSecret(WOLFSPDM_CTX* ctx,
294293
const byte* peerPubKeyX, const byte* peerPubKeyY);
295-
WOLFSPDM_API int wolfSPDM_GetRandom(WOLFSPDM_CTX* ctx, byte* out, word32 outSz);
296-
WOLFSPDM_API int wolfSPDM_SignHash(WOLFSPDM_CTX* ctx, const byte* hash, word32 hashSz,
294+
WOLFTPM_LOCAL int wolfSPDM_GetRandom(WOLFSPDM_CTX* ctx, byte* out, word32 outSz);
295+
WOLFTPM_LOCAL int wolfSPDM_SignHash(WOLFSPDM_CTX* ctx, const byte* hash, word32 hashSz,
297296
byte* sig, word32* sigSz);
298-
WOLFSPDM_API int wolfSPDM_VerifySignature(WOLFSPDM_CTX* ctx,
297+
WOLFTPM_LOCAL int wolfSPDM_VerifySignature(WOLFSPDM_CTX* ctx,
299298
const byte* hash, word32 hashSz,
300299
const byte* sig, word32 sigSz);
301300

302301
/* ----- Internal Function Declarations - Key Derivation ----- */
303302

304-
WOLFSPDM_API int wolfSPDM_DeriveHandshakeKeys(WOLFSPDM_CTX* ctx, const byte* th1Hash);
305-
WOLFSPDM_API int wolfSPDM_DeriveFromHandshakeSecret(WOLFSPDM_CTX* ctx, const byte* th1Hash);
306-
WOLFSPDM_API int wolfSPDM_DeriveAppDataKeys(WOLFSPDM_CTX* ctx);
307-
WOLFSPDM_API int wolfSPDM_HkdfExpandLabel(byte spdmVersion, const byte* secret, word32 secretSz,
303+
WOLFTPM_LOCAL int wolfSPDM_DeriveHandshakeKeys(WOLFSPDM_CTX* ctx, const byte* th1Hash);
304+
WOLFTPM_LOCAL int wolfSPDM_DeriveFromHandshakeSecret(WOLFSPDM_CTX* ctx, const byte* th1Hash);
305+
WOLFTPM_LOCAL int wolfSPDM_DeriveAppDataKeys(WOLFSPDM_CTX* ctx);
306+
WOLFTPM_LOCAL int wolfSPDM_HkdfExpandLabel(byte spdmVersion, const byte* secret, word32 secretSz,
308307
const char* label, const byte* context, word32 contextSz,
309308
byte* out, word32 outSz);
310-
WOLFSPDM_API int wolfSPDM_ComputeVerifyData(const byte* finishedKey, const byte* thHash,
309+
WOLFTPM_LOCAL int wolfSPDM_ComputeVerifyData(const byte* finishedKey, const byte* thHash,
311310
byte* verifyData);
312311

313312
/* ----- Internal Function Declarations - Message Building ----- */
314313

315-
WOLFSPDM_API int wolfSPDM_BuildGetVersion(byte* buf, word32* bufSz);
316-
WOLFSPDM_API int wolfSPDM_BuildKeyExchange(WOLFSPDM_CTX* ctx, byte* buf, word32* bufSz);
317-
WOLFSPDM_API int wolfSPDM_BuildFinish(WOLFSPDM_CTX* ctx, byte* buf, word32* bufSz);
318-
WOLFSPDM_API int wolfSPDM_BuildEndSession(WOLFSPDM_CTX* ctx, byte* buf, word32* bufSz);
314+
WOLFTPM_LOCAL int wolfSPDM_BuildGetVersion(byte* buf, word32* bufSz);
315+
WOLFTPM_LOCAL int wolfSPDM_BuildKeyExchange(WOLFSPDM_CTX* ctx, byte* buf, word32* bufSz);
316+
WOLFTPM_LOCAL int wolfSPDM_BuildFinish(WOLFSPDM_CTX* ctx, byte* buf, word32* bufSz);
317+
WOLFTPM_LOCAL int wolfSPDM_BuildEndSession(WOLFSPDM_CTX* ctx, byte* buf, word32* bufSz);
319318
/* PSK message builders/parsers declared in spdm_psk.h */
320319

321320
/* ----- Internal Function Declarations - Message Parsing ----- */
322321

323-
WOLFSPDM_API int wolfSPDM_ParseVersion(WOLFSPDM_CTX* ctx, const byte* buf, word32 bufSz);
324-
WOLFSPDM_API int wolfSPDM_ParseKeyExchangeRsp(WOLFSPDM_CTX* ctx, const byte* buf, word32 bufSz);
325-
WOLFSPDM_API int wolfSPDM_ParseFinishRsp(WOLFSPDM_CTX* ctx, const byte* buf, word32 bufSz);
326-
WOLFSPDM_API int wolfSPDM_CheckError(const byte* buf, word32 bufSz, int* errorCode);
322+
WOLFTPM_LOCAL int wolfSPDM_ParseVersion(WOLFSPDM_CTX* ctx, const byte* buf, word32 bufSz);
323+
WOLFTPM_LOCAL int wolfSPDM_ParseKeyExchangeRsp(WOLFSPDM_CTX* ctx, const byte* buf, word32 bufSz);
324+
WOLFTPM_LOCAL int wolfSPDM_ParseFinishRsp(WOLFSPDM_CTX* ctx, const byte* buf, word32 bufSz);
325+
WOLFTPM_LOCAL int wolfSPDM_CheckError(const byte* buf, word32 bufSz, int* errorCode);
327326

328327
/* ----- Internal Function Declarations - Secured Messaging ----- */
329328

330-
WOLFSPDM_API int wolfSPDM_EncryptInternal(WOLFSPDM_CTX* ctx,
329+
WOLFTPM_LOCAL int wolfSPDM_EncryptInternal(WOLFSPDM_CTX* ctx,
331330
const byte* plain, word32 plainSz,
332331
byte* enc, word32* encSz);
333-
WOLFSPDM_API int wolfSPDM_DecryptInternal(WOLFSPDM_CTX* ctx,
332+
WOLFTPM_LOCAL int wolfSPDM_DecryptInternal(WOLFSPDM_CTX* ctx,
334333
const byte* enc, word32 encSz,
335334
byte* plain, word32* plainSz);
336335

337336
/* ----- Internal Utility Functions ----- */
338337

339-
WOLFSPDM_API int wolfSPDM_SendReceive(WOLFSPDM_CTX* ctx,
338+
WOLFTPM_LOCAL int wolfSPDM_SendReceive(WOLFSPDM_CTX* ctx,
340339
const byte* txBuf, word32 txSz,
341340
byte* rxBuf, word32* rxSz);
342341

343342
#ifdef DEBUG_WOLFTPM
344-
WOLFSPDM_API void wolfSPDM_DebugPrint(WOLFSPDM_CTX* ctx, const char* fmt, ...)
343+
WOLFTPM_LOCAL void wolfSPDM_DebugPrint(WOLFSPDM_CTX* ctx, const char* fmt, ...)
345344
#ifdef __GNUC__
346345
__attribute__((format(printf, 2, 3)))
347346
#endif
348347
;
349348

350-
WOLFSPDM_API void wolfSPDM_DebugHex(WOLFSPDM_CTX* ctx, const char* label,
349+
WOLFTPM_LOCAL void wolfSPDM_DebugHex(WOLFSPDM_CTX* ctx, const char* label,
351350
const byte* data, word32 len);
352351
#else
353352
#define wolfSPDM_DebugPrint(ctx, fmt, ...) do { (void)(ctx); (void)fmt; } while(0)

src/spdm/spdm_kdf.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22+
#ifdef HAVE_CONFIG_H
23+
#include <config.h>
24+
#endif
25+
26+
#ifdef WOLFTPM_SPDM
27+
2228
#include "spdm_internal.h"
2329

2430
/* SPDM key derivation (DSP0277): HKDF with
@@ -262,3 +268,5 @@ int wolfSPDM_DeriveAppDataKeys(WOLFSPDM_CTX* ctx)
262268

263269
return rc;
264270
}
271+
272+
#endif /* WOLFTPM_SPDM */

src/spdm/spdm_msg.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22+
#ifdef HAVE_CONFIG_H
23+
#include <config.h>
24+
#endif
25+
26+
#ifdef WOLFTPM_SPDM
27+
2228
#include "spdm_internal.h"
2329

2430
int wolfSPDM_BuildGetVersion(byte* buf, word32* bufSz)
@@ -539,3 +545,5 @@ int wolfSPDM_ParseFinishRsp(WOLFSPDM_CTX* ctx, const byte* buf, word32 bufSz)
539545
}
540546

541547
/* PSK message builders/parsers moved to spdm_psk.c */
548+
549+
#endif /* WOLFTPM_SPDM */

src/spdm/spdm_nations.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22+
#ifdef HAVE_CONFIG_H
23+
#include <config.h>
24+
#endif
25+
26+
#ifdef WOLFTPM_SPDM
27+
2228
/* Nations Technology NS350 SPDM Functions
2329
*
2430
* PSK-mode vendor commands and PSK connection flow.
@@ -172,3 +178,5 @@ int wolfSPDM_Nations_PskClearWithVCA(WOLFSPDM_CTX* ctx,
172178
* wolfSPDM_ConnectNationsPsk is a backward-compat alias in spdm_psk.h. */
173179

174180
#endif /* WOLFSPDM_NATIONS */
181+
182+
#endif /* WOLFTPM_SPDM */

src/spdm/spdm_nuvoton.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22+
#ifdef HAVE_CONFIG_H
23+
#include <config.h>
24+
#endif
25+
26+
#ifdef WOLFTPM_SPDM
27+
2228
/* Nuvoton-specific SPDM functions (GetStatus, SetOnlyMode). */
2329

2430
#include "spdm_internal.h"
@@ -110,3 +116,5 @@ int wolfSPDM_Nuvoton_SetOnlyMode(
110116
}
111117

112118
#endif /* WOLFSPDM_NUVOTON */
119+
120+
#endif /* WOLFTPM_SPDM */

0 commit comments

Comments
 (0)