forked from wolfSSL/wolfssl
Add CryptoCb features (#6636)
* Update to support invoking cryptocb during un/register.
This commit is contained in:
@@ -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
|
||||
*/
|
||||
|
||||
|
||||
@@ -1299,9 +1302,13 @@ static const char* bench_result_words2[][5] = {
|
||||
#endif
|
||||
|
||||
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 */
|
||||
#ifdef WC_ENABLE_BENCH_THREADING
|
||||
@@ -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
|
||||
|
@@ -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
|
||||
@@ -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";
|
||||
@@ -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)
|
||||
@@ -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));
|
||||
}
|
||||
@@ -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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
@@ -267,16 +315,61 @@ int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx)
|
||||
dev->cb = cb;
|
||||
dev->ctx = ctx;
|
||||
|
||||
return 0;
|
||||
#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
|
||||
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
@@ -406,9 +415,13 @@ static void initDefaultName(void);
|
||||
/* for async devices */
|
||||
#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
|
||||
const char* wnrConfigFile = "wnr-example.conf";
|
||||
@@ -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) {
|
||||
|
@@ -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 */
|
||||
@@ -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
|
||||
@@ -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);
|
||||
|
Reference in New Issue
Block a user