Skip to content

Commit 0dca3bc

Browse files
committed
Setup to be opt-in for copy callback, and also added a outline for a free callback
1 parent 4d6418f commit 0dca3bc

7 files changed

Lines changed: 228 additions & 72 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ WOLFSSL_HARDEN_TLS_ALLOW_OLD_TLS
731731
WOLFSSL_HARDEN_TLS_ALLOW_TRUNCATED_HMAC
732732
WOLFSSL_HARDEN_TLS_NO_PKEY_CHECK
733733
WOLFSSL_HARDEN_TLS_NO_SCR_CHECK
734+
WOLFSSL_HAVE_COPY_FREE_CB
734735
WOLFSSL_HOSTNAME_VERIFY_ALT_NAME_ONLY
735736
WOLFSSL_I2D_ECDSA_SIG_ALLOC
736737
WOLFSSL_IAR_ARM_TIME

tests/api.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44807,6 +44807,77 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
4480744807
}
4480844808
#endif /* HAVE_ED25519 */
4480944809
}
44810+
#ifdef WOLFSSL_HAVE_COPY_FREE_CB
44811+
else if (info->algo_type == WC_ALGO_TYPE_COPY) {
44812+
#ifdef DEBUG_WOLFSSL
44813+
fprintf(stderr, "test_CryptoCb_Func: Copy Algo=%d Type=%d\n",
44814+
info->copy.algo, info->copy.type);
44815+
#endif
44816+
if (info->copy.algo == WC_ALGO_TYPE_HASH) {
44817+
switch (info->copy.type) {
44818+
#ifndef NO_SHA256
44819+
case WC_HASH_TYPE_SHA256:
44820+
{
44821+
wc_Sha256* src = (wc_Sha256*)info->copy.src;
44822+
wc_Sha256* dst = (wc_Sha256*)info->copy.dst;
44823+
/* set devId to invalid, so software is used */
44824+
src->devId = INVALID_DEVID;
44825+
ret = wc_Sha256Copy(src, dst);
44826+
44827+
/* reset devId */
44828+
src->devId = thisDevId;
44829+
if (ret == 0) {
44830+
/* Set the devId of the destination to the same */
44831+
/* since we used the software implementation of copy */
44832+
/* so dst would have been set to INVALID_DEVID */
44833+
dst->devId = thisDevId;
44834+
}
44835+
break;
44836+
}
44837+
#endif /* !NO_SHA256 */
44838+
default:
44839+
ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
44840+
break;
44841+
}
44842+
}
44843+
else {
44844+
ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
44845+
}
44846+
}
44847+
else if (info->algo_type == WC_ALGO_TYPE_FREE) {
44848+
#ifdef DEBUG_WOLFSSL
44849+
fprintf(stderr, "test_CryptoCb_Func: Free Algo=%d Type=%d\n",
44850+
info->free.algo, info->free.type);
44851+
#endif
44852+
44853+
if (info->free.algo == WC_ALGO_TYPE_HASH) {
44854+
switch (info->free.type) {
44855+
#ifndef NO_SHA256
44856+
case WC_HASH_TYPE_SHA256:
44857+
{
44858+
wc_Sha256* sha = (wc_Sha256*)info->free.obj;
44859+
44860+
/* set devId to invalid, so software is used */
44861+
sha->devId = INVALID_DEVID;
44862+
44863+
/* Call the actual free function */
44864+
wc_Sha256Free(sha);
44865+
44866+
/* Note: devId doesn't need to be restored as object is freed */
44867+
ret = 0;
44868+
break;
44869+
}
44870+
#endif
44871+
default:
44872+
ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
44873+
break;
44874+
}
44875+
}
44876+
else {
44877+
ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
44878+
}
44879+
}
44880+
#endif /* WOLFSSL_HAVE_COPY_FREE_CB */
4481044881
(void)thisDevId;
4481144882
(void)keyFormat;
4481244883

wolfcrypt/src/cryptocb.c

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ static const char* GetAlgoTypeStr(int algo)
8181
case WC_ALGO_TYPE_CMAC: return "CMAC";
8282
case WC_ALGO_TYPE_CERT: return "Cert";
8383
case WC_ALGO_TYPE_KDF: return "KDF";
84+
#ifdef WOLFSSL_HAVE_COPY_FREE_CB
8485
case WC_ALGO_TYPE_COPY: return "Copy";
86+
case WC_ALGO_TYPE_FREE: return "Free";
87+
#endif /* WOLFSSL_HAVE_COPY_FREE_CB */
8588
}
8689
return NULL;
8790
}
@@ -174,15 +177,6 @@ static const char* GetCryptoCbCmdTypeStr(int type)
174177
}
175178
#endif
176179

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 */
186180

187181
#if (defined(HAVE_HKDF) && !defined(NO_HMAC)) || defined(HAVE_CMAC_KDF)
188182
static const char* GetKdfTypeStr(int type)
@@ -263,13 +257,18 @@ void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
263257
GetCryptoCbCmdTypeStr(info->cmd.type), info->cmd.type);
264258
}
265259
#endif
266-
#ifndef NO_COPY_CB
260+
#ifdef WOLFSSL_HAVE_COPY_FREE_CB
267261
else if (info->algo_type == WC_ALGO_TYPE_COPY) {
268-
printf("Crypto CB: %s %s (%d)\n",
262+
printf("Crypto CB: %s %s Type=%d\n",
269263
GetAlgoTypeStr(info->algo_type),
270-
GetCryptoCbCopyTypeStr(info->copy.type), info->copy.type);
264+
GetAlgoTypeStr(info->copy.algo), info->copy.type);
271265
}
272-
#endif
266+
else if (info->algo_type == WC_ALGO_TYPE_FREE) {
267+
printf("Crypto CB: %s %s Type=%d\n",
268+
GetAlgoTypeStr(info->algo_type),
269+
GetAlgoTypeStr(info->free.algo), info->free.type);
270+
}
271+
#endif /* WOLFSSL_HAVE_COPY_FREE_CB */
273272
#if (defined(HAVE_HKDF) && !defined(NO_HMAC)) || \
274273
defined(HAVE_CMAC_KDF)
275274
else if (info->algo_type == WC_ALGO_TYPE_KDF) {
@@ -2045,40 +2044,65 @@ int wc_CryptoCb_Hkdf(int hashType, const byte* inKey, word32 inKeySz,
20452044
}
20462045
#endif /* HAVE_HKDF && !NO_HMAC */
20472046

2048-
#ifndef NO_COPY_CB
2047+
#ifdef WOLFSSL_HAVE_COPY_FREE_CB
20492048
/* General copy callback function for algorithm structures
20502049
* devId: The device ID to use for the callback
2051-
* copyType: The type of structure being copied (enum wc_CryptoCbCopyType)
2050+
* algo: Algorithm type (enum wc_AlgoType) - WC_ALGO_TYPE_HASH, WC_ALGO_TYPE_CIPHER, etc
2051+
* type: Specific type - for HASH: enum wc_HashType, for CIPHER: enum wc_CipherType
20522052
* src: Pointer to source structure
20532053
* dst: Pointer to destination structure
20542054
* Returns: 0 on success, negative on error, CRYPTOCB_UNAVAILABLE if not handled
20552055
*/
2056-
int wc_CryptoCb_Copy(int devId, int copyType, void* src, void* dst)
2056+
int wc_CryptoCb_Copy(int devId, int algo, int type, void* src, void* dst)
20572057
{
20582058
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
20592059
CryptoCb* dev;
20602060

2061-
/* Validate inputs */
2062-
if (src == NULL || dst == NULL) {
2063-
return BAD_FUNC_ARG;
2064-
}
2065-
20662061
/* Find registered callback device */
20672062
dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_COPY);
20682063
if (dev && dev->cb) {
20692064
wc_CryptoInfo cryptoInfo;
20702065
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
20712066
cryptoInfo.algo_type = WC_ALGO_TYPE_COPY;
2072-
cryptoInfo.copy.type = copyType;
2067+
cryptoInfo.copy.algo = algo;
2068+
cryptoInfo.copy.type = type;
20732069
cryptoInfo.copy.src = src;
20742070
cryptoInfo.copy.dst = dst;
2075-
2071+
20762072
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
20772073
}
2078-
2074+
2075+
return wc_CryptoCb_TranslateErrorCode(ret);
2076+
}
2077+
2078+
/* General free callback function for algorithm structures
2079+
* devId: The device ID to use for the callback
2080+
* algo: Algorithm type (enum wc_AlgoType) - WC_ALGO_TYPE_HASH, WC_ALGO_TYPE_CIPHER, etc
2081+
* type: Specific type - for HASH: enum wc_HashType, for CIPHER: enum wc_CipherType
2082+
* obj: Pointer to object structure to free
2083+
* Returns: 0 on success, negative on error, CRYPTOCB_UNAVAILABLE if not handled
2084+
*/
2085+
int wc_CryptoCb_Free(int devId, int algo, int type, void* obj)
2086+
{
2087+
int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
2088+
CryptoCb* dev;
2089+
2090+
/* Find registered callback device */
2091+
dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_FREE);
2092+
if (dev && dev->cb) {
2093+
wc_CryptoInfo cryptoInfo;
2094+
XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
2095+
cryptoInfo.algo_type = WC_ALGO_TYPE_FREE;
2096+
cryptoInfo.free.algo = algo;
2097+
cryptoInfo.free.type = type;
2098+
cryptoInfo.free.obj = obj;
2099+
2100+
ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
2101+
}
2102+
20792103
return wc_CryptoCb_TranslateErrorCode(ret);
20802104
}
2081-
#endif /* !NO_COPY_CB */
2105+
#endif /* WOLFSSL_HAVE_COPY_FREE_CB */
20822106

20832107

20842108
#if defined(HAVE_CMAC_KDF)

wolfcrypt/src/sha256.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,9 +2276,34 @@ int wc_InitSha256(wc_Sha256* sha256)
22762276

22772277
void wc_Sha256Free(wc_Sha256* sha256)
22782278
{
2279+
2280+
#if defined(WOLF_CRYPTO_CB) && defined(WOLFSSL_HAVE_COPY_FREE_CB)
2281+
int ret = 0;
2282+
#endif
2283+
22792284
if (sha256 == NULL)
22802285
return;
22812286

2287+
#if defined(WOLF_CRYPTO_CB) && defined(WOLFSSL_HAVE_COPY_FREE_CB)
2288+
#ifndef WOLF_CRYPTO_CB_FIND
2289+
if (sha256->devId != INVALID_DEVID)
2290+
#endif
2291+
{
2292+
ret = wc_CryptoCb_Free(sha256->devId, WC_ALGO_TYPE_HASH,
2293+
WC_HASH_TYPE_SHA256, (void*)sha256);
2294+
/* If they want the standard free, they can call it themselves */
2295+
/* via their callback setting devId to INVALID_DEVID */
2296+
/* otherwise assume the callback handled it */
2297+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2298+
return;
2299+
/* fall-through when unavailable */
2300+
}
2301+
2302+
/* silence compiler warning */
2303+
(void)ret;
2304+
2305+
#endif /* WOLF_CRYPTO_CB && WOLFSSL_HAVE_COPY_FREE_CB */
2306+
22822307
#if defined(WOLFSSL_ESP32) && \
22832308
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
22842309
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
@@ -2576,19 +2601,19 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
25762601
return BAD_FUNC_ARG;
25772602
}
25782603

2579-
#if defined(WOLF_CRYPTO_CB) && !defined(NO_COPY_CB)
2604+
#if defined(WOLF_CRYPTO_CB) && defined(WOLFSSL_HAVE_COPY_FREE_CB)
25802605
#ifndef WOLF_CRYPTO_CB_FIND
25812606
if (src->devId != INVALID_DEVID)
25822607
#endif
25832608
{
25842609
/* 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);
2610+
ret = wc_CryptoCb_Copy(src->devId, WC_ALGO_TYPE_HASH,
2611+
WC_HASH_TYPE_SHA256, (void*)src, (void*)dst);
25872612
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
25882613
return ret;
25892614
/* fall-through when unavailable */
25902615
}
2591-
#endif
2616+
#endif /* WOLF_CRYPTO_CB && WOLFSSL_HAVE_COPY_FREE_CB */
25922617

25932618
XMEMCPY(dst, src, sizeof(wc_Sha256));
25942619

wolfcrypt/test/test.c

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61295,43 +61295,79 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
6129561295
}
6129661296
}
6129761297
#endif /* !NO_SHA || !NO_SHA256 */
61298-
#ifndef NO_COPY_CB
61298+
#ifdef WOLFSSL_HAVE_COPY_FREE_CB
6129961299
else if (info->algo_type == WC_ALGO_TYPE_COPY) {
6130061300
#ifdef DEBUG_WOLFSSL
61301-
WOLFSSL_MSG_EX("CryptoDevCb: Copy Type %d\n", info->copy.type);
61301+
WOLFSSL_MSG_EX("CryptoDevCb: Copy Algo=%d Type=%d\n",
61302+
info->copy.algo, info->copy.type);
6130261303
#endif
61303-
switch (info->copy.type) {
61304+
if (info->copy.algo == WC_ALGO_TYPE_HASH) {
61305+
switch (info->copy.type) {
6130461306
#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);
61307+
case WC_HASH_TYPE_SHA256:
61308+
{
61309+
/* Cast the source and destination to the correct type */
61310+
/* Given as a void pointer initially for abstraction */
61311+
wc_Sha256* src = (wc_Sha256*)info->copy.src;
61312+
wc_Sha256* dst = (wc_Sha256*)info->copy.dst;
61313+
/* set devId to invalid, so software is used */
61314+
src->devId = INVALID_DEVID;
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+
}
6131661325

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;
61326+
break;
6132461327
}
61328+
#endif /* !NO_SHA256 */
61329+
default:
61330+
ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
61331+
break;
61332+
}
61333+
}
61334+
else {
61335+
ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
61336+
}
61337+
}
61338+
else if (info->algo_type == WC_ALGO_TYPE_FREE) {
61339+
#ifdef DEBUG_WOLFSSL
61340+
WOLFSSL_MSG_EX("CryptoDevCb: Free Algo=%d Type=%d\n",
61341+
info->free.algo, info->free.type);
61342+
#endif
6132561343

61326-
break;
61344+
if (info->free.algo == WC_ALGO_TYPE_HASH) {
61345+
switch (info->free.type) {
61346+
#ifndef NO_SHA256
61347+
case WC_HASH_TYPE_SHA256:
61348+
{
61349+
wc_Sha256* sha = (wc_Sha256*)info->free.obj;
61350+
/* set devId to invalid, so software is used */
61351+
sha->devId = INVALID_DEVID;
61352+
61353+
/* Call the actual free function */
61354+
wc_Sha256Free(sha);
61355+
61356+
/* Note: devId doesn't need to be restored as object is freed */
61357+
ret = 0;
61358+
break;
61359+
}
61360+
#endif
61361+
default:
61362+
ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
61363+
break;
6132761364
}
61328-
#endif /* !NO_SHA256 */
61329-
default:
61330-
ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
61331-
break;
61365+
}
61366+
else {
61367+
ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
6133261368
}
6133361369
}
61334-
#endif /* !NO_COPY_CB */
61370+
#endif /* WOLFSSL_HAVE_COPY_FREE_CB */
6133561371
#ifndef NO_HMAC
6133661372
else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
6133761373
if (info->hmac.hmac == NULL)

0 commit comments

Comments
 (0)