Skip to content

Commit 10adca1

Browse files
authored
Add CryptoCb features (#6636)
* Update to support invoking cryptocb during un/register.
1 parent c529b2f commit 10adca1

5 files changed

Lines changed: 152 additions & 11 deletions

File tree

wolfcrypt/benchmark/benchmark.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
* Enable tracking of the stats into an allocated linked list:
4141
* (use -print to display results):
4242
* WC_BENCH_TRACK_STATS
43+
*
44+
* set the default devId for cryptocb to the value instead of INVALID_DEVID
45+
* WC_USE_DEVID=0x1234
4346
*/
4447

4548

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

13011304
static THREAD_LS_T int devId = WOLFSSL_CAAM_DEVID;
13021305
#else
1306+
#ifdef WC_USE_DEVID
1307+
static THREAD_LS_T int devId = WC_USE_DEVID;
1308+
#else
13031309
static THREAD_LS_T int devId = INVALID_DEVID;
1310+
#endif
13041311
#endif
13051312

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

1315-
#if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_CAAM)
1322+
#if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_CAAM) || defined(WC_USE_DEVID)
13161323
#ifndef NO_HW_BENCH
13171324
#define BENCH_DEVID
13181325
#endif

wolfcrypt/src/cryptocb.c

Lines changed: 105 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@
2222
/* This framework provides a central place for crypto hardware integration
2323
using the devId scheme. If not supported return `CRYPTOCB_UNAVAILABLE`. */
2424

25+
/* Some common, optional build settings:
26+
* these can also be set in wolfssl/options.h or user_settings.h
27+
* -------------------------------------------------------------
28+
* enable the find device callback functions
29+
* WOLF_CRYPTO_CB_FIND
30+
*
31+
* enable the command callback functions to invoke the callback during
32+
* register and unregister
33+
* WOLF_CRYPTO_CB_CMD
34+
*
35+
* enable debug InfoString functions
36+
* DEBUG_CRYPTO_CB
37+
*/
38+
2539
#ifdef HAVE_CONFIG_H
2640
#include <config.h>
2741
#endif
@@ -62,6 +76,9 @@ static CryptoDevCallbackFind CryptoCb_FindCb = NULL;
6276
static const char* GetAlgoTypeStr(int algo)
6377
{
6478
switch (algo) { /* enum wc_AlgoType */
79+
#ifdef WOLF_CRYPTO_CB_CMD
80+
case WC_ALGO_TYPE_NONE: return "None-Command";
81+
#endif
6582
case WC_ALGO_TYPE_HASH: return "Hash";
6683
case WC_ALGO_TYPE_CIPHER: return "Cipher";
6784
case WC_ALGO_TYPE_PK: return "PK";
@@ -137,6 +154,14 @@ static const char* GetRsaType(int type)
137154
}
138155
#endif
139156

157+
static const char* GetCryptoCbCmdTypeStr(int type)
158+
{
159+
switch (type) {
160+
case WC_CRYPTOCB_CMD_TYPE_REGISTER: return "Register";
161+
case WC_CRYPTOCB_CMD_TYPE_UNREGISTER: return "UnRegister";
162+
}
163+
return NULL;
164+
}
140165
WOLFSSL_API void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
141166
{
142167
if (info == NULL)
@@ -169,6 +194,10 @@ WOLFSSL_API void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
169194
printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
170195
GetHashTypeStr(info->hmac.macType), info->hmac.macType);
171196
}
197+
else if (info->algo_type == WC_ALGO_TYPE_NONE) {
198+
printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
199+
GetCryptoCbCmdTypeStr(info->cmd.type), info->cmd.type);
200+
}
172201
else {
173202
printf("CryptoCb: %s \n", GetAlgoTypeStr(info->algo_type));
174203
}
@@ -223,11 +252,28 @@ static WC_INLINE int wc_CryptoCb_TranslateErrorCode(int ret)
223252
return ret;
224253
}
225254

255+
/* Helper function to reset a device entry to invalid */
256+
static WC_INLINE void wc_CryptoCb_ClearDev(CryptoCb *dev)
257+
{
258+
XMEMSET(dev, 0, sizeof(*dev));
259+
dev->devId = INVALID_DEVID;
260+
}
261+
226262
void wc_CryptoCb_Init(void)
227263
{
228264
int i;
229-
for (i=0; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) {
230-
gCryptoDev[i].devId = INVALID_DEVID;
265+
for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) {
266+
wc_CryptoCb_ClearDev(&gCryptoDev[i]);
267+
}
268+
}
269+
270+
void wc_CryptoCb_Cleanup(void)
271+
{
272+
int i;
273+
for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) {
274+
if(gCryptoDev[i].devId != INVALID_DEVID) {
275+
wc_CryptoCb_UnRegisterDevice(gCryptoDev[i].devId);
276+
}
231277
}
232278
}
233279

@@ -255,6 +301,8 @@ void wc_CryptoCb_SetDeviceFindCb(CryptoDevCallbackFind cb)
255301

256302
int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx)
257303
{
304+
int rc = 0;
305+
258306
/* find existing or new */
259307
CryptoCb* dev = wc_CryptoCb_GetDevice(devId);
260308
if (dev == NULL)
@@ -264,19 +312,64 @@ int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx)
264312
return BUFFER_E; /* out of devices */
265313

266314
dev->devId = devId;
267-
dev->cb = cb;
268-
dev->ctx = ctx;
269-
270-
return 0;
315+
dev->cb = cb;
316+
dev->ctx = ctx;
317+
318+
#ifdef WOLF_CRYPTO_CB_CMD
319+
if (cb != NULL) {
320+
/* Invoke callback with register command */
321+
wc_CryptoInfo info;
322+
XMEMSET(&info, 0, sizeof(info));
323+
info.algo_type = WC_ALGO_TYPE_NONE;
324+
info.cmd.type = WC_CRYPTOCB_CMD_TYPE_REGISTER;
325+
info.cmd.ctx = ctx; /* cb may update on success */
326+
327+
rc = cb(devId, &info, ctx);
328+
if (rc == 0) {
329+
/* Success. Update dev->ctx */
330+
dev->ctx = info.cmd.ctx;
331+
}
332+
else if ((rc == CRYPTOCB_UNAVAILABLE) ||
333+
(rc == NOT_COMPILED_IN)) {
334+
/* Not implemented. Return success*/
335+
rc = 0;
336+
}
337+
else {
338+
/* Error in callback register cmd. Don't register */
339+
wc_CryptoCb_ClearDev(dev);
340+
}
341+
}
342+
#endif
343+
return rc;
271344
}
272345

273346
void wc_CryptoCb_UnRegisterDevice(int devId)
274347
{
275-
CryptoCb* dev = wc_CryptoCb_GetDevice(devId);
276-
if (dev) {
277-
XMEMSET(dev, 0, sizeof(*dev));
278-
dev->devId = INVALID_DEVID;
348+
CryptoCb* dev = NULL;
349+
350+
/* Can't unregister the invalid device */
351+
if (devId == INVALID_DEVID)
352+
return;
353+
354+
/* Find the matching dev */
355+
dev = wc_CryptoCb_GetDevice(devId);
356+
if (dev == NULL)
357+
return;
358+
359+
#ifdef WOLF_CRYPTO_CB_CMD
360+
if (dev->cb != NULL) {
361+
/* Invoke callback with unregister command.*/
362+
wc_CryptoInfo info;
363+
XMEMSET(&info, 0, sizeof(info));
364+
info.algo_type = WC_ALGO_TYPE_NONE;
365+
info.cmd.type = WC_CRYPTOCB_CMD_TYPE_UNREGISTER;
366+
info.cmd.ctx = NULL; /* Not used */
367+
368+
/* Ignore errors here */
369+
dev->cb(devId, &info, dev->ctx);
279370
}
371+
#endif
372+
wc_CryptoCb_ClearDev(dev);
280373
}
281374

282375
#ifndef NO_RSA
@@ -1343,6 +1436,8 @@ int wc_CryptoCb_DefaultDevID(void)
13431436
ret = WOLFSSL_CAAM_DEVID;
13441437
#elif defined(HAVE_ARIA)
13451438
ret = WOLFSSL_ARIA_DEVID;
1439+
#elif defined(WC_USE_DEVID)
1440+
ret = WC_USE_DEVID;
13461441
#else
13471442
ret = INVALID_DEVID;
13481443
#endif

wolfcrypt/src/wc_port.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ int wolfCrypt_Cleanup(void)
486486
Entropy_Final();
487487
#endif
488488

489+
#ifdef WOLF_CRYPTO_CB
490+
wc_CryptoCb_Cleanup();
491+
#endif
492+
489493
#if defined(WOLFSSL_MEM_FAIL_COUNT) && defined(WOLFCRYPT_ONLY)
490494
wc_MemFailCount_Free();
491495
#endif

wolfcrypt/test/test.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22+
/*
23+
* Some common, optional build settings:
24+
* these can also be set in wolfssl/options.h or user_settings.h
25+
* -------------------------------------------------------------
26+
*
27+
* set the default devId for cryptocb to the value instead of INVALID_DEVID
28+
* WC_USE_DEVID=0x1234
29+
*/
30+
2231
#ifdef HAVE_CONFIG_H
2332
#include <config.h>
2433
#endif
@@ -407,7 +416,11 @@ static void initDefaultName(void);
407416
#ifdef WOLFSSL_CAAM_DEVID
408417
static int devId = WOLFSSL_CAAM_DEVID;
409418
#else
419+
#ifdef WC_USE_DEVID
420+
static int devId = WC_USE_DEVID;
421+
#else
410422
static int devId = INVALID_DEVID;
423+
#endif
411424
#endif
412425

413426
#ifdef HAVE_WNR
@@ -879,6 +892,10 @@ wc_test_ret_t wolfcrypt_test(void* args)
879892

880893
printf("------------------------------------------------------------------------------\n");
881894
printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
895+
#ifdef WOLF_CRYPTO_CB
896+
if (devId != INVALID_DEVID)
897+
printf(" CryptoCB with DevID:%X\n", devId);
898+
#endif
882899
printf("------------------------------------------------------------------------------\n");
883900

884901
if (args) {

wolfssl/wolfcrypt/cryptocb.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@
7272
#include <wolfssl/wolfcrypt/sha512.h>
7373
#endif
7474

75+
#ifdef WOLF_CRYPTO_CB_CMD
76+
/* CryptoCb Commands */
77+
enum wc_CryptoCbCmdType {
78+
WC_CRYPTOCB_CMD_TYPE_NONE = 0,
79+
WC_CRYPTOCB_CMD_TYPE_REGISTER,
80+
WC_CRYPTOCB_CMD_TYPE_UNREGISTER,
81+
82+
WC_CRYPTOCB_CMD_TYPE_MAX = WC_CRYPTOCB_CMD_TYPE_UNREGISTER
83+
};
84+
#endif
85+
7586
/* Crypto Information Structure for callbacks */
7687
typedef struct wc_CryptoInfo {
7788
int algo_type; /* enum wc_AlgoType */
@@ -356,6 +367,12 @@ typedef struct wc_CryptoInfo {
356367
int type;
357368
} cmac;
358369
#endif
370+
#ifdef WOLF_CRYPTO_CB_CMD
371+
struct { /* uses wc_AlgoType=ALGO_NONE */
372+
int type; /* enum wc_CryptoCbCmdType */
373+
void *ctx;
374+
} cmd;
375+
#endif
359376
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
360377
};
361378
#endif
@@ -365,6 +382,7 @@ typedef struct wc_CryptoInfo {
365382
typedef int (*CryptoDevCallbackFunc)(int devId, wc_CryptoInfo* info, void* ctx);
366383

367384
WOLFSSL_LOCAL void wc_CryptoCb_Init(void);
385+
WOLFSSL_LOCAL void wc_CryptoCb_Cleanup(void);
368386
WOLFSSL_LOCAL int wc_CryptoCb_GetDevIdAtIndex(int startIdx);
369387
WOLFSSL_API int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx);
370388
WOLFSSL_API void wc_CryptoCb_UnRegisterDevice(int devId);

0 commit comments

Comments
 (0)