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;
6276static 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+ }
140165WOLFSSL_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+
226262void 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
256302int 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
273346void 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
0 commit comments