Address review: narrow CryptoCb introspection to a boolean check

- Replace public wc_CryptoCb_GetDevice() with
  wc_CryptoCb_IsDeviceRegistered() returns 1 or 0. keep the CryptoCb
  struct and GetDevice private.
- Reject RegisterDevice(INVALID_DEVID) with BAD_FUNC_ARG instead of ALREADY_E.
- Document the new API and the ALREADY_E/BAD_FUNC_ARG returns.
- Fix table-full test to not leak when MAX_CRYPTO_DEVID_CALLBACKS >= 256.
This commit is contained in:
Alex Lanzano
2026-06-10 15:02:22 -04:00
parent 451eaf9cba
commit c0ec8f39b2
4 changed files with 75 additions and 31 deletions
+19 -4
View File
@@ -80,10 +80,12 @@ Crypto Callback Build Options:
#include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
#endif
/* TODO: Consider linked list with mutex */
#ifndef MAX_CRYPTO_DEVID_CALLBACKS
#define MAX_CRYPTO_DEVID_CALLBACKS 8
#endif
typedef struct CryptoCb {
int devId;
CryptoDevCallbackFunc cb;
void* ctx;
} CryptoCb;
static WC_THREADSHARED CryptoCb gCryptoDev[MAX_CRYPTO_DEVID_CALLBACKS];
#ifdef WOLF_CRYPTO_CB_FIND
@@ -350,7 +352,7 @@ void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
/* Search through listed devices and return the first matching device ID
* found. */
CryptoCb* wc_CryptoCb_GetDevice(int devId)
static CryptoCb* wc_CryptoCb_GetDevice(int devId)
{
int i;
for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) {
@@ -360,6 +362,15 @@ CryptoCb* wc_CryptoCb_GetDevice(int devId)
return NULL;
}
/* Returns 1 if the given device ID is currently registered, 0 otherwise.
* INVALID_DEVID marks free table slots, so it is never reported registered. */
int wc_CryptoCb_IsDeviceRegistered(int devId)
{
if (devId == INVALID_DEVID)
return 0;
return wc_CryptoCb_GetDevice(devId) != NULL;
}
/* Filters through find callback set when trying to get the device,
* returns the device found on success and null if not found. */
@@ -448,6 +459,10 @@ int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx)
int rc = 0;
CryptoCb* dev;
/* INVALID_DEVID marks a free slot and cannot be registered as a device. */
if (devId == INVALID_DEVID)
return BAD_FUNC_ARG;
/* Reject re-registration of an already-registered device ID. */
if (wc_CryptoCb_GetDevice(devId) != NULL)
return ALREADY_E;