Skip to content

Commit

Permalink
Add CryptoCb features (#6636)
Browse files Browse the repository at this point in the history
* Update to support invoking cryptocb during un/register.
  • Loading branch information
billphipps authored Jul 27, 2023
1 parent c529b2f commit 10adca1
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 11 deletions.
9 changes: 8 additions & 1 deletion wolfcrypt/benchmark/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
* Enable tracking of the stats into an allocated linked list:
* (use -print to display results):
* WC_BENCH_TRACK_STATS
*
* set the default devId for cryptocb to the value instead of INVALID_DEVID
* WC_USE_DEVID=0x1234
*/


Expand Down Expand Up @@ -1300,7 +1303,11 @@ static const char* bench_result_words2[][5] = {

static THREAD_LS_T int devId = WOLFSSL_CAAM_DEVID;
#else
#ifdef WC_USE_DEVID
static THREAD_LS_T int devId = WC_USE_DEVID;
#else
static THREAD_LS_T int devId = INVALID_DEVID;
#endif
#endif

/* Asynchronous helper macros */
Expand All @@ -1312,7 +1319,7 @@ static const char* bench_result_words2[][5] = {
static volatile int g_threadCount;
#endif

#if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_CAAM)
#if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_CAAM) || defined(WC_USE_DEVID)
#ifndef NO_HW_BENCH
#define BENCH_DEVID
#endif
Expand Down
115 changes: 105 additions & 10 deletions wolfcrypt/src/cryptocb.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
/* This framework provides a central place for crypto hardware integration
using the devId scheme. If not supported return `CRYPTOCB_UNAVAILABLE`. */

/* Some common, optional build settings:
* these can also be set in wolfssl/options.h or user_settings.h
* -------------------------------------------------------------
* enable the find device callback functions
* WOLF_CRYPTO_CB_FIND
*
* enable the command callback functions to invoke the callback during
* register and unregister
* WOLF_CRYPTO_CB_CMD
*
* enable debug InfoString functions
* DEBUG_CRYPTO_CB
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
Expand Down Expand Up @@ -62,6 +76,9 @@ static CryptoDevCallbackFind CryptoCb_FindCb = NULL;
static const char* GetAlgoTypeStr(int algo)
{
switch (algo) { /* enum wc_AlgoType */
#ifdef WOLF_CRYPTO_CB_CMD
case WC_ALGO_TYPE_NONE: return "None-Command";
#endif
case WC_ALGO_TYPE_HASH: return "Hash";
case WC_ALGO_TYPE_CIPHER: return "Cipher";
case WC_ALGO_TYPE_PK: return "PK";
Expand Down Expand Up @@ -137,6 +154,14 @@ static const char* GetRsaType(int type)
}
#endif

static const char* GetCryptoCbCmdTypeStr(int type)
{
switch (type) {
case WC_CRYPTOCB_CMD_TYPE_REGISTER: return "Register";
case WC_CRYPTOCB_CMD_TYPE_UNREGISTER: return "UnRegister";
}
return NULL;
}
WOLFSSL_API void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
{
if (info == NULL)
Expand Down Expand Up @@ -169,6 +194,10 @@ WOLFSSL_API void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
GetHashTypeStr(info->hmac.macType), info->hmac.macType);
}
else if (info->algo_type == WC_ALGO_TYPE_NONE) {
printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
GetCryptoCbCmdTypeStr(info->cmd.type), info->cmd.type);
}
else {
printf("CryptoCb: %s \n", GetAlgoTypeStr(info->algo_type));
}
Expand Down Expand Up @@ -223,11 +252,28 @@ static WC_INLINE int wc_CryptoCb_TranslateErrorCode(int ret)
return ret;
}

/* Helper function to reset a device entry to invalid */
static WC_INLINE void wc_CryptoCb_ClearDev(CryptoCb *dev)
{
XMEMSET(dev, 0, sizeof(*dev));
dev->devId = INVALID_DEVID;
}

void wc_CryptoCb_Init(void)
{
int i;
for (i=0; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) {
gCryptoDev[i].devId = INVALID_DEVID;
for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) {
wc_CryptoCb_ClearDev(&gCryptoDev[i]);
}
}

void wc_CryptoCb_Cleanup(void)
{
int i;
for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) {
if(gCryptoDev[i].devId != INVALID_DEVID) {
wc_CryptoCb_UnRegisterDevice(gCryptoDev[i].devId);
}
}
}

Expand Down Expand Up @@ -255,6 +301,8 @@ void wc_CryptoCb_SetDeviceFindCb(CryptoDevCallbackFind cb)

int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx)
{
int rc = 0;

/* find existing or new */
CryptoCb* dev = wc_CryptoCb_GetDevice(devId);
if (dev == NULL)
Expand All @@ -264,19 +312,64 @@ int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx)
return BUFFER_E; /* out of devices */

dev->devId = devId;
dev->cb = cb;
dev->ctx = ctx;

return 0;
dev->cb = cb;
dev->ctx = ctx;

#ifdef WOLF_CRYPTO_CB_CMD
if (cb != NULL) {
/* Invoke callback with register command */
wc_CryptoInfo info;
XMEMSET(&info, 0, sizeof(info));
info.algo_type = WC_ALGO_TYPE_NONE;
info.cmd.type = WC_CRYPTOCB_CMD_TYPE_REGISTER;
info.cmd.ctx = ctx; /* cb may update on success */

rc = cb(devId, &info, ctx);
if (rc == 0) {
/* Success. Update dev->ctx */
dev->ctx = info.cmd.ctx;
}
else if ((rc == CRYPTOCB_UNAVAILABLE) ||
(rc == NOT_COMPILED_IN)) {
/* Not implemented. Return success*/
rc = 0;
}
else {
/* Error in callback register cmd. Don't register */
wc_CryptoCb_ClearDev(dev);
}
}
#endif
return rc;
}

void wc_CryptoCb_UnRegisterDevice(int devId)
{
CryptoCb* dev = wc_CryptoCb_GetDevice(devId);
if (dev) {
XMEMSET(dev, 0, sizeof(*dev));
dev->devId = INVALID_DEVID;
CryptoCb* dev = NULL;

/* Can't unregister the invalid device */
if (devId == INVALID_DEVID)
return;

/* Find the matching dev */
dev = wc_CryptoCb_GetDevice(devId);
if (dev == NULL)
return;

#ifdef WOLF_CRYPTO_CB_CMD
if (dev->cb != NULL) {
/* Invoke callback with unregister command.*/
wc_CryptoInfo info;
XMEMSET(&info, 0, sizeof(info));
info.algo_type = WC_ALGO_TYPE_NONE;
info.cmd.type = WC_CRYPTOCB_CMD_TYPE_UNREGISTER;
info.cmd.ctx = NULL; /* Not used */

/* Ignore errors here */
dev->cb(devId, &info, dev->ctx);
}
#endif
wc_CryptoCb_ClearDev(dev);
}

#ifndef NO_RSA
Expand Down Expand Up @@ -1343,6 +1436,8 @@ int wc_CryptoCb_DefaultDevID(void)
ret = WOLFSSL_CAAM_DEVID;
#elif defined(HAVE_ARIA)
ret = WOLFSSL_ARIA_DEVID;
#elif defined(WC_USE_DEVID)
ret = WC_USE_DEVID;
#else
ret = INVALID_DEVID;
#endif
Expand Down
4 changes: 4 additions & 0 deletions wolfcrypt/src/wc_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@ int wolfCrypt_Cleanup(void)
Entropy_Final();
#endif

#ifdef WOLF_CRYPTO_CB
wc_CryptoCb_Cleanup();
#endif

#if defined(WOLFSSL_MEM_FAIL_COUNT) && defined(WOLFCRYPT_ONLY)
wc_MemFailCount_Free();
#endif
Expand Down
17 changes: 17 additions & 0 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/

/*
* Some common, optional build settings:
* these can also be set in wolfssl/options.h or user_settings.h
* -------------------------------------------------------------
*
* set the default devId for cryptocb to the value instead of INVALID_DEVID
* WC_USE_DEVID=0x1234
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
Expand Down Expand Up @@ -407,7 +416,11 @@ static void initDefaultName(void);
#ifdef WOLFSSL_CAAM_DEVID
static int devId = WOLFSSL_CAAM_DEVID;
#else
#ifdef WC_USE_DEVID
static int devId = WC_USE_DEVID;
#else
static int devId = INVALID_DEVID;
#endif
#endif

#ifdef HAVE_WNR
Expand Down Expand Up @@ -879,6 +892,10 @@ wc_test_ret_t wolfcrypt_test(void* args)

printf("------------------------------------------------------------------------------\n");
printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
#ifdef WOLF_CRYPTO_CB
if (devId != INVALID_DEVID)
printf(" CryptoCB with DevID:%X\n", devId);
#endif
printf("------------------------------------------------------------------------------\n");

if (args) {
Expand Down
18 changes: 18 additions & 0 deletions wolfssl/wolfcrypt/cryptocb.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@
#include <wolfssl/wolfcrypt/sha512.h>
#endif

#ifdef WOLF_CRYPTO_CB_CMD
/* CryptoCb Commands */
enum wc_CryptoCbCmdType {
WC_CRYPTOCB_CMD_TYPE_NONE = 0,
WC_CRYPTOCB_CMD_TYPE_REGISTER,
WC_CRYPTOCB_CMD_TYPE_UNREGISTER,

WC_CRYPTOCB_CMD_TYPE_MAX = WC_CRYPTOCB_CMD_TYPE_UNREGISTER
};
#endif

/* Crypto Information Structure for callbacks */
typedef struct wc_CryptoInfo {
int algo_type; /* enum wc_AlgoType */
Expand Down Expand Up @@ -356,6 +367,12 @@ typedef struct wc_CryptoInfo {
int type;
} cmac;
#endif
#ifdef WOLF_CRYPTO_CB_CMD
struct { /* uses wc_AlgoType=ALGO_NONE */
int type; /* enum wc_CryptoCbCmdType */
void *ctx;
} cmd;
#endif
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
};
#endif
Expand All @@ -365,6 +382,7 @@ typedef struct wc_CryptoInfo {
typedef int (*CryptoDevCallbackFunc)(int devId, wc_CryptoInfo* info, void* ctx);

WOLFSSL_LOCAL void wc_CryptoCb_Init(void);
WOLFSSL_LOCAL void wc_CryptoCb_Cleanup(void);
WOLFSSL_LOCAL int wc_CryptoCb_GetDevIdAtIndex(int startIdx);
WOLFSSL_API int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx);
WOLFSSL_API void wc_CryptoCb_UnRegisterDevice(int devId);
Expand Down

0 comments on commit 10adca1

Please sign in to comment.