Skip to content

Commit 658dbd4

Browse files
committed
Fix for m33mu NV
1 parent df956fb commit 658dbd4

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

examples/run_examples.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,11 @@ run_tpm_tls_client() { # Usage: run_tpm_tls_client [ecc/rsa] [tpmargs] [tlsversi
450450
pushd $WOLFSSL_PATH >> $TPMPWD/run.out 2>&1
451451
echo -e "./examples/server/server -v $3 -p $port -w -g -A ./certs/tpm-ca-$1-cert.pem -R $READY_FILE"
452452
./examples/server/server -v $3 -p $port -w -g -A ./certs/tpm-ca-$1-cert.pem -R "$READY_FILE" >> $TPMPWD/run.out 2>&1 &
453+
SERVER_PID=$!
453454
popd >> $TPMPWD/run.out 2>&1
454455
if ! wait_for_ready "$READY_FILE" 500; then
455456
echo -e "wolfSSL server failed to start for $1 $2"
457+
kill $SERVER_PID 2>/dev/null
456458
rm -f "$READY_FILE"
457459
exit 1
458460
fi
@@ -470,8 +472,10 @@ run_tpm_tls_server() { # Usage: run_tpm_tls_server [ecc/rsa] [tpmargs] [tlsversi
470472

471473
echo -e "./examples/tls/tls_server -p=$port -$1 $2"
472474
./examples/tls/tls_server -p=$port -$1 $2 >> $TPMPWD/run.out 2>&1 &
475+
SERVER_PID=$!
473476
if ! wait_for_port "$port" 500; then
474477
echo -e "TPM TLS server failed to start on port $port for $1 $2"
478+
kill $SERVER_PID 2>/dev/null
475479
exit 1
476480
fi
477481
pushd $WOLFSSL_PATH >> $TPMPWD/run.out 2>&1

scripts/fwtpm_emu_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ echo " TZEN: $TZEN"
7272
if [ $DO_BUILD -eq 1 ]; then
7373
echo "Building fwTPM STM32 (TZEN=$TZEN, SELFTEST=1)..."
7474
make -C "$PORT_DIR" clean > /dev/null 2>&1
75-
if ! make -C "$PORT_DIR" WOLFTPM_DIR="$WOLFTPM_DIR" ${WOLFSSL_DIR:+WOLFSSL_DIR="$WOLFSSL_DIR"} TZEN=$TZEN SELFTEST=1 > /tmp/fwtpm_emu_build.log 2>&1; then
75+
if ! make -C "$PORT_DIR" WOLFTPM_DIR="$WOLFTPM_DIR" ${WOLFSSL_DIR:+WOLFSSL_DIR="$WOLFSSL_DIR"} TZEN=$TZEN SELFTEST=1 EXTRA_CFLAGS="-DFWTPM_NO_NV" > /tmp/fwtpm_emu_build.log 2>&1; then
7676
echo "FAIL: Build failed"
7777
tail -20 /tmp/fwtpm_emu_build.log
7878
exit 1

src/fwtpm/fwtpm.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,32 @@ int FWTPM_Init(FWTPM_CTX* ctx)
9292
}
9393

9494
/* Initialize NV storage - loads existing state or creates fresh seeds */
95+
#ifndef FWTPM_NO_NV
9596
rc = FWTPM_NV_Init(ctx);
9697
if (rc != 0) {
9798
wc_FreeRng(&ctx->rng);
9899
wolfCrypt_Cleanup();
99100
return rc;
100101
}
102+
#else
103+
/* No NV: generate ephemeral seeds (lost on reset) */
104+
rc = wc_RNG_GenerateBlock(&ctx->rng, ctx->ownerSeed, FWTPM_SEED_SIZE);
105+
if (rc == 0)
106+
rc = wc_RNG_GenerateBlock(&ctx->rng, ctx->endorsementSeed,
107+
FWTPM_SEED_SIZE);
108+
if (rc == 0)
109+
rc = wc_RNG_GenerateBlock(&ctx->rng, ctx->platformSeed,
110+
FWTPM_SEED_SIZE);
111+
if (rc == 0)
112+
rc = wc_RNG_GenerateBlock(&ctx->rng, ctx->nullSeed,
113+
FWTPM_SEED_SIZE);
114+
if (rc != 0) {
115+
wc_FreeRng(&ctx->rng);
116+
wolfCrypt_Cleanup();
117+
return TPM_RC_FAILURE;
118+
}
119+
ctx->pcrAllocatedBanks = FWTPM_PCR_ALLOC_DEFAULT;
120+
#endif
101121

102122
return rc;
103123
}
@@ -111,7 +131,11 @@ int FWTPM_Cleanup(FWTPM_CTX* ctx)
111131
}
112132

113133
/* Save NV state before cleanup */
134+
#ifndef FWTPM_NO_NV
114135
rc = FWTPM_NV_Save(ctx);
136+
#else
137+
rc = TPM_RC_SUCCESS;
138+
#endif
115139

116140
wc_FreeRng(&ctx->rng);
117141
wolfCrypt_Cleanup();

src/fwtpm/fwtpm_nv.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,8 @@ int FWTPM_NV_Init(FWTPM_CTX* ctx)
11231123
rc = FWTPM_NV_Save(ctx);
11241124
#ifdef DEBUG_WOLFTPM
11251125
if (rc != TPM_RC_SUCCESS) {
1126-
printf("fwTPM: Warning: Failed to save initial NV state\n");
1126+
printf("fwTPM: Warning: Failed to save initial NV state (%d)\n",
1127+
rc);
11271128
}
11281129
#endif
11291130
}

src/tpm2_swtpm.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ static TPM_RC SwTpmConnect(TPM2_CTX* ctx, const char* host, const char* port)
210210

211211
/* 8N1: 8 data bits, no parity, 1 stop bit */
212212
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
213-
tty.c_cflag &= ~(PARENB | PARODD | CSTOPB | CRTSCTS);
213+
tty.c_cflag &= ~(PARENB | PARODD | CSTOPB);
214+
#ifdef CRTSCTS
215+
tty.c_cflag &= ~CRTSCTS;
216+
#endif
214217
tty.c_cflag |= (CLOCAL | CREAD);
215218

216219
/* Raw mode: no special input/output processing */

tests/fwtpm_check.sh

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ SKIP_EXAMPLES=0
2929
# --- Helpers ---
3030

3131
# Wait for a TCP port to be listening
32-
# Uses ss to check without connecting (nc -z would consume the accept slot)
32+
# Uses ss/netstat to check without connecting (nc -z would consume the accept slot)
3333
wait_for_port() {
3434
local port="$1" timeout="${2:-500}" elapsed=0
3535
while [ $elapsed -lt $timeout ]; do
36-
if ss -tln 2>/dev/null | grep -q ":${port} "; then
36+
if command -v ss >/dev/null 2>&1; then
37+
ss -tln 2>/dev/null | grep -q ":${port} " && return 0
38+
elif netstat -tln 2>/dev/null | grep -q ":${port} "; then
3739
return 0
3840
fi
3941
sleep 0.01
@@ -42,6 +44,21 @@ wait_for_port() {
4244
return 1
4345
}
4446

47+
# Check if a port is in use (returns 0 if port is in use)
48+
check_port_in_use() {
49+
local port="$1"
50+
if command -v nc >/dev/null 2>&1; then
51+
nc -z localhost "$port" 2>/dev/null
52+
return $?
53+
elif command -v ss >/dev/null 2>&1; then
54+
ss -tln 2>/dev/null | grep -q ":${port} "
55+
return $?
56+
elif netstat -tln 2>/dev/null | grep -q ":${port} "; then
57+
return 0
58+
fi
59+
return 1 # no tool available, assume in use to be safe
60+
}
61+
4562
# Pick an available random port (returns port on stdout)
4663
pick_available_port() {
4764
local port attempts=0
@@ -51,7 +68,7 @@ pick_available_port() {
5168
else
5269
port=$(( (RANDOM % 55000) + 10000 ))
5370
fi
54-
if ! nc -z localhost "$port" 2>/dev/null; then
71+
if ! check_port_in_use "$port"; then
5572
echo "$port"
5673
return 0
5774
fi
@@ -240,7 +257,7 @@ if [ $IS_FWTPM_MODE -eq 1 ]; then
240257
# --- fwTPM mode: we manage the server lifecycle ---
241258

242259
# Check if a server is already running (e.g. started by CI)
243-
if [ $IS_SWTPM_MODE -eq 1 ] && ss -tln 2>/dev/null | grep -q ":${FWTPM_PORT} "; then
260+
if [ $IS_SWTPM_MODE -eq 1 ] && check_port_in_use "$FWTPM_PORT"; then
244261
echo "Server already running on port $FWTPM_PORT"
245262
if [ $HAS_GETENV -eq 1 ]; then
246263
export TPM2_SWTPM_PORT="$FWTPM_PORT"
@@ -312,7 +329,7 @@ else
312329
export TPM2_SWTPM_PORT="$FWTPM_PORT"
313330
fi
314331

315-
if ! ss -tln 2>/dev/null | grep -q ":${FWTPM_PORT} "; then
332+
if ! check_port_in_use "$FWTPM_PORT"; then
316333
echo "No TPM server on port $FWTPM_PORT, skipping (start one with: tpm_server &)"
317334
exit 77
318335
fi

0 commit comments

Comments
 (0)