mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-04 13:14:45 +02:00
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:
|
* Enable tracking of the stats into an allocated linked list:
|
||||||
* (use -print to display results):
|
* (use -print to display results):
|
||||||
* WC_BENCH_TRACK_STATS
|
* WC_BENCH_TRACK_STATS
|
||||||
|
*
|
||||||
|
* set the default devId for cryptocb to the value instead of INVALID_DEVID
|
||||||
|
* WC_USE_DEVID=0x1234
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@@ -1300,7 +1303,11 @@ static const char* bench_result_words2[][5] = {
|
|||||||
|
|
||||||
static THREAD_LS_T int devId = WOLFSSL_CAAM_DEVID;
|
static THREAD_LS_T int devId = WOLFSSL_CAAM_DEVID;
|
||||||
#else
|
#else
|
||||||
|
#ifdef WC_USE_DEVID
|
||||||
|
static THREAD_LS_T int devId = WC_USE_DEVID;
|
||||||
|
#else
|
||||||
static THREAD_LS_T int devId = INVALID_DEVID;
|
static THREAD_LS_T int devId = INVALID_DEVID;
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Asynchronous helper macros */
|
/* Asynchronous helper macros */
|
||||||
@@ -1312,7 +1319,7 @@ static const char* bench_result_words2[][5] = {
|
|||||||
static volatile int g_threadCount;
|
static volatile int g_threadCount;
|
||||||
#endif
|
#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
|
#ifndef NO_HW_BENCH
|
||||||
#define BENCH_DEVID
|
#define BENCH_DEVID
|
||||||
#endif
|
#endif
|
||||||
|
@@ -22,6 +22,20 @@
|
|||||||
/* This framework provides a central place for crypto hardware integration
|
/* This framework provides a central place for crypto hardware integration
|
||||||
using the devId scheme. If not supported return `CRYPTOCB_UNAVAILABLE`. */
|
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
|
#ifdef HAVE_CONFIG_H
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -62,6 +76,9 @@ static CryptoDevCallbackFind CryptoCb_FindCb = NULL;
|
|||||||
static const char* GetAlgoTypeStr(int algo)
|
static const char* GetAlgoTypeStr(int algo)
|
||||||
{
|
{
|
||||||
switch (algo) { /* enum wc_AlgoType */
|
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_HASH: return "Hash";
|
||||||
case WC_ALGO_TYPE_CIPHER: return "Cipher";
|
case WC_ALGO_TYPE_CIPHER: return "Cipher";
|
||||||
case WC_ALGO_TYPE_PK: return "PK";
|
case WC_ALGO_TYPE_PK: return "PK";
|
||||||
@@ -137,6 +154,14 @@ static const char* GetRsaType(int type)
|
|||||||
}
|
}
|
||||||
#endif
|
#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)
|
WOLFSSL_API void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
|
||||||
{
|
{
|
||||||
if (info == NULL)
|
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),
|
printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
|
||||||
GetHashTypeStr(info->hmac.macType), info->hmac.macType);
|
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 {
|
else {
|
||||||
printf("CryptoCb: %s \n", GetAlgoTypeStr(info->algo_type));
|
printf("CryptoCb: %s \n", GetAlgoTypeStr(info->algo_type));
|
||||||
}
|
}
|
||||||
@@ -223,11 +252,28 @@ static WC_INLINE int wc_CryptoCb_TranslateErrorCode(int ret)
|
|||||||
return 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)
|
void wc_CryptoCb_Init(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<MAX_CRYPTO_DEVID_CALLBACKS; 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 wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx)
|
||||||
{
|
{
|
||||||
|
int rc = 0;
|
||||||
|
|
||||||
/* find existing or new */
|
/* find existing or new */
|
||||||
CryptoCb* dev = wc_CryptoCb_GetDevice(devId);
|
CryptoCb* dev = wc_CryptoCb_GetDevice(devId);
|
||||||
if (dev == NULL)
|
if (dev == NULL)
|
||||||
@@ -264,19 +312,64 @@ int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx)
|
|||||||
return BUFFER_E; /* out of devices */
|
return BUFFER_E; /* out of devices */
|
||||||
|
|
||||||
dev->devId = devId;
|
dev->devId = devId;
|
||||||
dev->cb = cb;
|
dev->cb = cb;
|
||||||
dev->ctx = ctx;
|
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)
|
void wc_CryptoCb_UnRegisterDevice(int devId)
|
||||||
{
|
{
|
||||||
CryptoCb* dev = wc_CryptoCb_GetDevice(devId);
|
CryptoCb* dev = NULL;
|
||||||
if (dev) {
|
|
||||||
XMEMSET(dev, 0, sizeof(*dev));
|
/* Can't unregister the invalid device */
|
||||||
dev->devId = INVALID_DEVID;
|
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
|
#ifndef NO_RSA
|
||||||
@@ -1343,6 +1436,8 @@ int wc_CryptoCb_DefaultDevID(void)
|
|||||||
ret = WOLFSSL_CAAM_DEVID;
|
ret = WOLFSSL_CAAM_DEVID;
|
||||||
#elif defined(HAVE_ARIA)
|
#elif defined(HAVE_ARIA)
|
||||||
ret = WOLFSSL_ARIA_DEVID;
|
ret = WOLFSSL_ARIA_DEVID;
|
||||||
|
#elif defined(WC_USE_DEVID)
|
||||||
|
ret = WC_USE_DEVID;
|
||||||
#else
|
#else
|
||||||
ret = INVALID_DEVID;
|
ret = INVALID_DEVID;
|
||||||
#endif
|
#endif
|
||||||
|
@@ -486,6 +486,10 @@ int wolfCrypt_Cleanup(void)
|
|||||||
Entropy_Final();
|
Entropy_Final();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef WOLF_CRYPTO_CB
|
||||||
|
wc_CryptoCb_Cleanup();
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(WOLFSSL_MEM_FAIL_COUNT) && defined(WOLFCRYPT_ONLY)
|
#if defined(WOLFSSL_MEM_FAIL_COUNT) && defined(WOLFCRYPT_ONLY)
|
||||||
wc_MemFailCount_Free();
|
wc_MemFailCount_Free();
|
||||||
#endif
|
#endif
|
||||||
|
@@ -19,6 +19,15 @@
|
|||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
* 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
|
#ifdef HAVE_CONFIG_H
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -407,7 +416,11 @@ static void initDefaultName(void);
|
|||||||
#ifdef WOLFSSL_CAAM_DEVID
|
#ifdef WOLFSSL_CAAM_DEVID
|
||||||
static int devId = WOLFSSL_CAAM_DEVID;
|
static int devId = WOLFSSL_CAAM_DEVID;
|
||||||
#else
|
#else
|
||||||
|
#ifdef WC_USE_DEVID
|
||||||
|
static int devId = WC_USE_DEVID;
|
||||||
|
#else
|
||||||
static int devId = INVALID_DEVID;
|
static int devId = INVALID_DEVID;
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_WNR
|
#ifdef HAVE_WNR
|
||||||
@@ -879,6 +892,10 @@ wc_test_ret_t wolfcrypt_test(void* args)
|
|||||||
|
|
||||||
printf("------------------------------------------------------------------------------\n");
|
printf("------------------------------------------------------------------------------\n");
|
||||||
printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
|
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");
|
printf("------------------------------------------------------------------------------\n");
|
||||||
|
|
||||||
if (args) {
|
if (args) {
|
||||||
|
@@ -72,6 +72,17 @@
|
|||||||
#include <wolfssl/wolfcrypt/sha512.h>
|
#include <wolfssl/wolfcrypt/sha512.h>
|
||||||
#endif
|
#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 */
|
/* Crypto Information Structure for callbacks */
|
||||||
typedef struct wc_CryptoInfo {
|
typedef struct wc_CryptoInfo {
|
||||||
int algo_type; /* enum wc_AlgoType */
|
int algo_type; /* enum wc_AlgoType */
|
||||||
@@ -356,6 +367,12 @@ typedef struct wc_CryptoInfo {
|
|||||||
int type;
|
int type;
|
||||||
} cmac;
|
} cmac;
|
||||||
#endif
|
#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
|
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
@@ -365,6 +382,7 @@ typedef struct wc_CryptoInfo {
|
|||||||
typedef int (*CryptoDevCallbackFunc)(int devId, wc_CryptoInfo* info, void* ctx);
|
typedef int (*CryptoDevCallbackFunc)(int devId, wc_CryptoInfo* info, void* ctx);
|
||||||
|
|
||||||
WOLFSSL_LOCAL void wc_CryptoCb_Init(void);
|
WOLFSSL_LOCAL void wc_CryptoCb_Init(void);
|
||||||
|
WOLFSSL_LOCAL void wc_CryptoCb_Cleanup(void);
|
||||||
WOLFSSL_LOCAL int wc_CryptoCb_GetDevIdAtIndex(int startIdx);
|
WOLFSSL_LOCAL int wc_CryptoCb_GetDevIdAtIndex(int startIdx);
|
||||||
WOLFSSL_API int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx);
|
WOLFSSL_API int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx);
|
||||||
WOLFSSL_API void wc_CryptoCb_UnRegisterDevice(int devId);
|
WOLFSSL_API void wc_CryptoCb_UnRegisterDevice(int devId);
|
||||||
|
Reference in New Issue
Block a user