From 71819bcb44fbb7b24e5fbf80487f59d90e88f583 Mon Sep 17 00:00:00 2001 From: rizlik Date: Wed, 6 May 2026 09:42:26 +0200 Subject: [PATCH] swdev: pair internal wolfCrypt_Init() with wolfCrypt_Cleanup() --- tests/swdev/Makefile | 8 +++++--- tests/swdev/swdev.c | 8 ++++++++ tests/swdev/swdev.h | 3 ++- tests/swdev/swdev_loader.c | 4 +++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/swdev/Makefile b/tests/swdev/Makefile index 561e1f9bbd..971beb6e1e 100644 --- a/tests/swdev/Makefile +++ b/tests/swdev/Makefile @@ -116,10 +116,12 @@ $(BUILDDIR)/swdev.o: $(BUILDDIR)/swdev.partial.o @# visibility("default") and overrides the command-line @# -fvisibility=hidden. --keep-global-symbol sidesteps that by matching @# on symbol name directly. - $(OBJCOPY) --keep-global-symbol=wc_SwDev_Callback $< $@ - @# Sanity-check: only wc_SwDev_Callback may be externally visible. + $(OBJCOPY) --keep-global-symbol=wc_SwDev_Callback \ + --keep-global-symbol=wc_SwDev_InternalCleanup $< $@ + @# Sanity-check: only the two swdev entry points may be externally visible. @visible=$$( $(NM) --extern-only --defined-only $@ | awk '{print $$3}' \ - | grep -v '^wc_SwDev_Callback$$' || true); \ + | grep -v '^wc_SwDev_Callback$$' \ + | grep -v '^wc_SwDev_InternalCleanup$$' || true); \ if [ -n "$$visible" ]; then \ echo "error: unexpected externally-visible symbols in $@:"; \ echo "$$visible"; \ diff --git a/tests/swdev/swdev.c b/tests/swdev/swdev.c index ff4c0f5d99..981e55ec14 100644 --- a/tests/swdev/swdev.c +++ b/tests/swdev/swdev.c @@ -54,6 +54,14 @@ static int swdev_ensure_init(void) return 0; } +WC_SWDEV_EXPORT void wc_SwDev_InternalCleanup(void) +{ + if (swdev_initialized) { + wolfCrypt_Cleanup(); + swdev_initialized = 0; + } +} + #ifndef NO_RSA static int swdev_rsa(wc_CryptoInfo* info) { diff --git a/tests/swdev/swdev.h b/tests/swdev/swdev.h index b974f496f2..50f02b9f66 100644 --- a/tests/swdev/swdev.h +++ b/tests/swdev/swdev.h @@ -38,8 +38,9 @@ extern "C" { #define WC_SWDEV_EXPORT #endif -WC_SWDEV_EXPORT int wc_SwDev_Callback(int devId, wc_CryptoInfo* info, +WC_SWDEV_EXPORT int wc_SwDev_Callback(int devId, wc_CryptoInfo* info, void* ctx); +WC_SWDEV_EXPORT void wc_SwDev_InternalCleanup(void); #ifdef __cplusplus } diff --git a/tests/swdev/swdev_loader.c b/tests/swdev/swdev_loader.c index 8d4c1f82b7..ee6f859688 100644 --- a/tests/swdev/swdev_loader.c +++ b/tests/swdev/swdev_loader.c @@ -30,7 +30,8 @@ #endif /* resolved at link time from swdev.o */ -extern int wc_SwDev_Callback(int devId, wc_CryptoInfo* info, void* ctx); +extern int wc_SwDev_Callback(int devId, wc_CryptoInfo* info, void* ctx); +extern void wc_SwDev_InternalCleanup(void); /* Init always (re)registers because wolfCrypt_Cleanup wipes the cryptocb * table; only the final Cleanup unregisters. */ @@ -61,6 +62,7 @@ void wc_SwDev_Cleanup(void) wc_CryptoCb_SetDeviceFindCb(NULL); #endif wc_CryptoCb_UnRegisterDevice(WC_SWDEV_ID); + wc_SwDev_InternalCleanup(); } else if (my_refCount < 0) { /* called more times than Init; restore floor */