diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index e9a7ed7310..c29d6eafc2 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -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 */ @@ -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 */ @@ -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 diff --git a/wolfcrypt/src/cryptocb.c b/wolfcrypt/src/cryptocb.c index 7b56fe812e..5b1b9e66e3 100644 --- a/wolfcrypt/src/cryptocb.c +++ b/wolfcrypt/src/cryptocb.c @@ -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 #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; idevId = 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 @@ -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 diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index e89efb5107..367d609276 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -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 diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index d4ea8845e6..6e0a4cce39 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -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 #endif @@ -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 @@ -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) { diff --git a/wolfssl/wolfcrypt/cryptocb.h b/wolfssl/wolfcrypt/cryptocb.h index c1b4307fe7..0f468c4e6a 100644 --- a/wolfssl/wolfcrypt/cryptocb.h +++ b/wolfssl/wolfcrypt/cryptocb.h @@ -72,6 +72,17 @@ #include #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);