Skip to content

Commit 4d6418f

Browse files
committed
Add crypto callback support for copy operations (SHA-256)
1 parent 7fa53c8 commit 4d6418f

5 files changed

Lines changed: 128 additions & 3 deletions

File tree

wolfcrypt/src/cryptocb.c

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ static const char* GetAlgoTypeStr(int algo)
8080
case WC_ALGO_TYPE_HMAC: return "HMAC";
8181
case WC_ALGO_TYPE_CMAC: return "CMAC";
8282
case WC_ALGO_TYPE_CERT: return "Cert";
83-
case WC_ALGO_TYPE_KDF:
84-
return "KDF";
83+
case WC_ALGO_TYPE_KDF: return "KDF";
84+
case WC_ALGO_TYPE_COPY: return "Copy";
8585
}
8686
return NULL;
8787
}
@@ -174,6 +174,16 @@ static const char* GetCryptoCbCmdTypeStr(int type)
174174
}
175175
#endif
176176

177+
#ifndef NO_COPY_CB
178+
static const char* GetCryptoCbCopyTypeStr(int type)
179+
{
180+
switch (type) {
181+
case WC_CRYPTOCB_COPY_TYPE_SHA256: return "SHA256-Copy";
182+
}
183+
return NULL;
184+
}
185+
#endif /* !NO_COPY_CB */
186+
177187
#if (defined(HAVE_HKDF) && !defined(NO_HMAC)) || defined(HAVE_CMAC_KDF)
178188
static const char* GetKdfTypeStr(int type)
179189
{
@@ -253,6 +263,13 @@ void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
253263
GetCryptoCbCmdTypeStr(info->cmd.type), info->cmd.type);
254264
}
255265
#endif
266+
#ifndef NO_COPY_CB
267+
else if (info->algo_type == WC_ALGO_TYPE_COPY) {
268+
printf("Crypto CB: %s %s (%d)\n",
269+
GetAlgoTypeStr(info->algo_type),
270+
GetCryptoCbCopyTypeStr(info->copy.type), info->copy.type);
271+
}
272+
#endif
256273
#if (defined(HAVE_HKDF) && !defined(NO_HMAC)) || \
257274
defined(HAVE_CMAC_KDF)
258275
else if (info->algo_type == WC_ALGO_TYPE_KDF) {
@@ -2028,6 +2045,41 @@ int wc_CryptoCb_Hkdf(int hashType, const byte* inKey, word32 inKeySz,
20282045
}
20292046
#endif /* HAVE_HKDF && !NO_HMAC */
20302047

2048+
#ifndef NO_COPY_CB
2049+
/* General copy callback function for algorithm structures
2050+
* devId: The device ID to use for the callback
2051+
* copyType: The type of structure being copied (enum wc_CryptoCbCopyType)
2052+
* src: Pointer to source structure
2053+
* dst: Pointer to destination structure
2054+
* Returns: 0 on success, negative on error, CRYPTOCB_UNAVAILABLE if not handled
2055+
*/
2056+
int wc_CryptoCb_Copy(int devId, int copyType, void* src, void* dst)
2057+
{
2058+
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
2059+
CryptoCb* dev;
2060+
2061+
/* Validate inputs */
2062+
if (src == NULL || dst == NULL) {
2063+
return BAD_FUNC_ARG;
2064+
}
2065+
2066+
/* Find registered callback device */
2067+
dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_COPY);
2068+
if (dev && dev->cb) {
2069+
wc_CryptoInfo cryptoInfo;
2070+
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
2071+
cryptoInfo.algo_type = WC_ALGO_TYPE_COPY;
2072+
cryptoInfo.copy.type = copyType;
2073+
cryptoInfo.copy.src = src;
2074+
cryptoInfo.copy.dst = dst;
2075+
2076+
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
2077+
}
2078+
2079+
return wc_CryptoCb_TranslateErrorCode(ret);
2080+
}
2081+
#endif /* !NO_COPY_CB */
2082+
20312083

20322084
#if defined(HAVE_CMAC_KDF)
20332085
/* Crypto callback for NIST SP 800 56C two-step CMAC KDF. See software

wolfcrypt/src/sha256.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,6 +2576,20 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
25762576
return BAD_FUNC_ARG;
25772577
}
25782578

2579+
#if defined(WOLF_CRYPTO_CB) && !defined(NO_COPY_CB)
2580+
#ifndef WOLF_CRYPTO_CB_FIND
2581+
if (src->devId != INVALID_DEVID)
2582+
#endif
2583+
{
2584+
/* Cast the source and destination to be void to keep the abstraction */
2585+
ret = wc_CryptoCb_Copy(src->devId, WC_CRYPTOCB_COPY_TYPE_SHA256,
2586+
(void*)src, (void*)dst);
2587+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2588+
return ret;
2589+
/* fall-through when unavailable */
2590+
}
2591+
#endif
2592+
25792593
XMEMCPY(dst, src, sizeof(wc_Sha256));
25802594

25812595
#ifdef WOLFSSL_MAXQ10XX_CRYPTO

wolfcrypt/test/test.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61295,6 +61295,43 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
6129561295
}
6129661296
}
6129761297
#endif /* !NO_SHA || !NO_SHA256 */
61298+
#ifndef NO_COPY_CB
61299+
else if (info->algo_type == WC_ALGO_TYPE_COPY) {
61300+
#ifdef DEBUG_WOLFSSL
61301+
WOLFSSL_MSG_EX("CryptoDevCb: Copy Type %d\n", info->copy.type);
61302+
#endif
61303+
switch (info->copy.type) {
61304+
#ifndef NO_SHA256
61305+
case WC_CRYPTOCB_COPY_TYPE_SHA256:
61306+
{
61307+
/* Cast the source and destination to the correct type */
61308+
/* Given as a void pointer initally for abstraction */
61309+
wc_Sha256* src = (wc_Sha256*)info->copy.src;
61310+
wc_Sha256* dst = (wc_Sha256*)info->copy.dst;
61311+
61312+
/* set devId to invalid, so software is used */
61313+
src->devId = INVALID_DEVID;
61314+
61315+
ret = wc_Sha256Copy(src, dst);
61316+
61317+
/* reset devId */
61318+
src->devId = devIdArg;
61319+
if (ret == 0) {
61320+
/* Set the devId of the destination to the same as the */
61321+
/* since we used the software implementation of copy */
61322+
/* so dst would have been set to INVALID_DEVID */
61323+
dst->devId = devIdArg;
61324+
}
61325+
61326+
break;
61327+
}
61328+
#endif /* !NO_SHA256 */
61329+
default:
61330+
ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
61331+
break;
61332+
}
61333+
}
61334+
#endif /* !NO_COPY_CB */
6129861335
#ifndef NO_HMAC
6129961336
else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
6130061337
if (info->hmac.hmac == NULL)

wolfssl/wolfcrypt/cryptocb.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ enum wc_CryptoCbCmdType {
101101
};
102102
#endif
103103

104+
#ifndef NO_COPY_CB
105+
/* CryptoCb Copy Types - for copy operations on algorithm structures */
106+
enum wc_CryptoCbCopyType {
107+
WC_CRYPTOCB_COPY_TYPE_NONE = 0,
108+
WC_CRYPTOCB_COPY_TYPE_SHA256,
109+
WC_CRYPTOCB_COPY_TYPE_MAX = WC_CRYPTOCB_COPY_TYPE_SHA256
110+
};
111+
#endif /* !NO_COPY_CB */
112+
104113

105114
#if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
106115
typedef struct {
@@ -468,6 +477,13 @@ typedef struct wc_CryptoInfo {
468477
void *ctx;
469478
} cmd;
470479
#endif
480+
#ifndef NO_COPY_CB
481+
struct { /* uses wc_AlgoType=WC_ALGO_TYPE_COPY */
482+
int type; /* enum wc_CryptoCbCopyType */
483+
void *src; /* Source structure to copy from */
484+
void *dst; /* Destination structure to copy to */
485+
} copy;
486+
#endif
471487
#if defined(HAVE_HKDF) || defined(HAVE_CMAC_KDF)
472488
struct {
473489
int type; /* enum wc_KdfType */
@@ -737,6 +753,11 @@ WOLFSSL_LOCAL int wc_CryptoCb_GetCert(int devId, const char *label,
737753
word32* outSz, int *format, void *heap);
738754
#endif
739755

756+
#ifndef NO_COPY_CB
757+
WOLFSSL_LOCAL int wc_CryptoCb_Copy(int devId, int copyType, void* src,
758+
void* dst);
759+
#endif
760+
740761
#endif /* WOLF_CRYPTO_CB */
741762

742763
#ifdef __cplusplus

wolfssl/wolfcrypt/types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,8 +1309,9 @@ enum wc_AlgoType {
13091309
WC_ALGO_TYPE_CMAC = 7,
13101310
WC_ALGO_TYPE_CERT = 8,
13111311
WC_ALGO_TYPE_KDF = 9,
1312+
WC_ALGO_TYPE_COPY = 10,
13121313

1313-
WC_ALGO_TYPE_MAX = WC_ALGO_TYPE_KDF
1314+
WC_ALGO_TYPE_MAX = WC_ALGO_TYPE_COPY
13141315
};
13151316

13161317
/* KDF types */

0 commit comments

Comments
 (0)