Skip to content

Commit 33313ed

Browse files
committed
Error out when trying to register a a cryptocb with an existing devId
1 parent e572523 commit 33313ed

3 files changed

Lines changed: 53 additions & 9 deletions

File tree

tests/api.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28976,13 +28976,14 @@ static int test_wc_CryptoCb(void)
2897628976
(!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519))
2897728977
int tlsVer;
2897828978
#endif
28979-
/* TODO: Add crypto callback API tests */
28980-
28981-
/* Test wc_CryptoCb_GetDevice() lookup behavior. */
28979+
/* Exercise the exposed CryptoCb device-management API:
28980+
* wc_CryptoCb_RegisterDevice / UnRegisterDevice / GetDevice /
28981+
* DefaultDevID (and InfoString when DEBUG_CRYPTOCB is enabled). */
2898228982
{
2898328983
int getDevId = 1234;
2898428984
int getDevCtx = 0;
2898528985
CryptoCb* dev = NULL;
28986+
int i, n, rc;
2898628987

2898728988
/* Unregistered devId is not found. */
2898828989
ExpectNull(wc_CryptoCb_GetDevice(getDevId));
@@ -28999,9 +29000,50 @@ static int test_wc_CryptoCb(void)
2899929000
/* A different, unregistered devId is still not found. */
2900029001
ExpectNull(wc_CryptoCb_GetDevice(getDevId + 1));
2900129002

29003+
/* Re-registering an already-registered devId is rejected. */
29004+
ExpectIntEQ(wc_CryptoCb_RegisterDevice(getDevId, NULL, &getDevCtx),
29005+
WC_NO_ERR_TRACE(ALREADY_E));
29006+
29007+
/* wc_CryptoCb_DefaultDevID behavior depends on the build config. */
29008+
#ifdef WC_NO_DEFAULT_DEVID
29009+
ExpectIntEQ(wc_CryptoCb_DefaultDevID(), INVALID_DEVID);
29010+
#elif !defined(WOLFSSL_CAAM_DEVID) && !defined(HAVE_ARIA) && \
29011+
!defined(WC_USE_DEVID)
29012+
/* "first available" mode: a device is registered, so one is returned. */
29013+
ExpectIntNE(wc_CryptoCb_DefaultDevID(), INVALID_DEVID);
29014+
#endif
29015+
2900229016
/* After unregistering, the device is no longer found. */
2900329017
wc_CryptoCb_UnRegisterDevice(getDevId);
2900429018
ExpectNull(wc_CryptoCb_GetDevice(getDevId));
29019+
29020+
/* Unregistering is a harmless no-op for unknown or invalid devIds. */
29021+
wc_CryptoCb_UnRegisterDevice(getDevId); /* already removed */
29022+
wc_CryptoCb_UnRegisterDevice(INVALID_DEVID); /* never a real devId */
29023+
ExpectNull(wc_CryptoCb_GetDevice(getDevId));
29024+
29025+
/* The device table is finite: once full, registration returns
29026+
* BUFFER_E. Register unique devIds until that happens, then clean up.
29027+
* The cap (256) just guards against an unexpectedly large table. */
29028+
rc = 0;
29029+
for (i = 0; rc == 0 && i < 256; i++) {
29030+
rc = wc_CryptoCb_RegisterDevice(0x5000 + i, NULL, NULL);
29031+
}
29032+
ExpectIntEQ(rc, WC_NO_ERR_TRACE(BUFFER_E));
29033+
/* Every id except the final failed attempt registered; unregister them. */
29034+
for (n = 0; n + 1 < i; n++) {
29035+
wc_CryptoCb_UnRegisterDevice(0x5000 + n);
29036+
}
29037+
29038+
#ifdef DEBUG_CRYPTOCB
29039+
/* wc_CryptoCb_InfoString smoke test: must not dereference bad memory. */
29040+
{
29041+
wc_CryptoInfo info;
29042+
XMEMSET(&info, 0, sizeof(info));
29043+
info.algo_type = WC_ALGO_TYPE_NONE;
29044+
wc_CryptoCb_InfoString(&info);
29045+
}
29046+
#endif
2900529047
}
2900629048

2900729049
#ifdef HAVE_IO_TESTS_DEPENDENCIES

wolfcrypt/src/cryptocb.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,14 @@ void wc_CryptoCb_SetDeviceFindCb(CryptoDevCallbackFind cb)
436436
int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx)
437437
{
438438
int rc = 0;
439+
CryptoCb* dev;
439440

440-
/* find existing or new */
441-
CryptoCb* dev = wc_CryptoCb_GetDevice(devId);
442-
if (dev == NULL)
443-
dev = wc_CryptoCb_GetDevice(INVALID_DEVID);
441+
/* Reject re-registration of an already-registered device ID. */
442+
if (wc_CryptoCb_GetDevice(devId) != NULL)
443+
return ALREADY_E;
444444

445+
/* find a free slot */
446+
dev = wc_CryptoCb_GetDevice(INVALID_DEVID);
445447
if (dev == NULL)
446448
return BUFFER_E; /* out of devices */
447449

wolfssl/version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
extern "C" {
2929
#endif
3030

31-
#define LIBWOLFSSL_VERSION_STRING "5.8.4"
32-
#define LIBWOLFSSL_VERSION_HEX 0x05008004
31+
#define LIBWOLFSSL_VERSION_STRING "5.9.1"
32+
#define LIBWOLFSSL_VERSION_HEX 0x05009001
3333

3434
#ifdef __cplusplus
3535
}

0 commit comments

Comments
 (0)