Skip to content

Commit daf3b06

Browse files
committed
Add common SHA copy/free helpers with leak-safe msg buffer handling and copy/free crypto callbacks to replicate the non-callback code behavior when using MAX3266X_SHA_CB.
1 parent c3b329e commit daf3b06

2 files changed

Lines changed: 233 additions & 56 deletions

File tree

wolfcrypt/src/port/maxim/max3266x.c

Lines changed: 208 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,133 @@ int wc_MxcShaCryptoCb(wc_CryptoInfo* info)
204204
}
205205
#endif /* MAX3266X_SHA_CB */
206206

207+
#ifdef WOLF_CRYPTO_CB_COPY
208+
static int wc_MxcCopyCb(wc_CryptoInfo* info)
209+
{
210+
if (info == NULL || info->copy.src == NULL || info->copy.dst == NULL) {
211+
return BAD_FUNC_ARG;
212+
}
213+
214+
switch (info->copy.type) {
215+
#ifdef MAX3266X_SHA_CB
216+
#ifndef NO_SHA
217+
case WC_HASH_TYPE_SHA:
218+
return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst,
219+
sizeof(wc_Sha),
220+
&((wc_Sha*)info->copy.dst)->msg,
221+
&((wc_Sha*)info->copy.dst)->used,
222+
&((wc_Sha*)info->copy.dst)->len,
223+
((wc_Sha*)info->copy.dst)->heap,
224+
((wc_Sha*)info->copy.src)->heap);
225+
#endif
226+
#ifdef WOLFSSL_SHA224
227+
case WC_HASH_TYPE_SHA224:
228+
return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst,
229+
sizeof(wc_Sha224),
230+
&((wc_Sha224*)info->copy.dst)->msg,
231+
&((wc_Sha224*)info->copy.dst)->used,
232+
&((wc_Sha224*)info->copy.dst)->len,
233+
((wc_Sha224*)info->copy.dst)->heap,
234+
((wc_Sha224*)info->copy.src)->heap);
235+
#endif
236+
#ifndef NO_SHA256
237+
case WC_HASH_TYPE_SHA256:
238+
return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst,
239+
sizeof(wc_Sha256),
240+
&((wc_Sha256*)info->copy.dst)->msg,
241+
&((wc_Sha256*)info->copy.dst)->used,
242+
&((wc_Sha256*)info->copy.dst)->len,
243+
((wc_Sha256*)info->copy.dst)->heap,
244+
((wc_Sha256*)info->copy.src)->heap);
245+
#endif
246+
#ifdef WOLFSSL_SHA384
247+
case WC_HASH_TYPE_SHA384:
248+
return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst,
249+
sizeof(wc_Sha384),
250+
&((wc_Sha384*)info->copy.dst)->msg,
251+
&((wc_Sha384*)info->copy.dst)->used,
252+
&((wc_Sha384*)info->copy.dst)->len,
253+
((wc_Sha384*)info->copy.dst)->heap,
254+
((wc_Sha384*)info->copy.src)->heap);
255+
#endif
256+
#ifdef WOLFSSL_SHA512
257+
case WC_HASH_TYPE_SHA512:
258+
return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst,
259+
sizeof(wc_Sha512),
260+
&((wc_Sha512*)info->copy.dst)->msg,
261+
&((wc_Sha512*)info->copy.dst)->used,
262+
&((wc_Sha512*)info->copy.dst)->len,
263+
((wc_Sha512*)info->copy.dst)->heap,
264+
((wc_Sha512*)info->copy.src)->heap);
265+
#endif
266+
#endif /* MAX3266X_SHA_CB */
267+
default:
268+
return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
269+
}
270+
}
271+
#endif /* WOLF_CRYPTO_CB_COPY */
272+
273+
#ifdef WOLF_CRYPTO_CB_FREE
274+
static int wc_MxcFreeCb(wc_CryptoInfo* info)
275+
{
276+
if (info == NULL || info->free.obj == NULL) {
277+
return BAD_FUNC_ARG;
278+
}
279+
280+
switch (info->free.type) {
281+
#ifdef MAX3266X_SHA_CB
282+
#ifndef NO_SHA
283+
case WC_HASH_TYPE_SHA:
284+
wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha),
285+
&((wc_Sha*)info->free.obj)->msg,
286+
&((wc_Sha*)info->free.obj)->used,
287+
&((wc_Sha*)info->free.obj)->len,
288+
((wc_Sha*)info->free.obj)->heap);
289+
return 0;
290+
#endif
291+
#ifdef WOLFSSL_SHA224
292+
case WC_HASH_TYPE_SHA224:
293+
wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha224),
294+
&((wc_Sha224*)info->free.obj)->msg,
295+
&((wc_Sha224*)info->free.obj)->used,
296+
&((wc_Sha224*)info->free.obj)->len,
297+
((wc_Sha224*)info->free.obj)->heap);
298+
return 0;
299+
#endif
300+
#ifndef NO_SHA256
301+
case WC_HASH_TYPE_SHA256:
302+
wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha256),
303+
&((wc_Sha256*)info->free.obj)->msg,
304+
&((wc_Sha256*)info->free.obj)->used,
305+
&((wc_Sha256*)info->free.obj)->len,
306+
((wc_Sha256*)info->free.obj)->heap);
307+
return 0;
308+
#endif
309+
#ifdef WOLFSSL_SHA384
310+
case WC_HASH_TYPE_SHA384:
311+
wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha384),
312+
&((wc_Sha384*)info->free.obj)->msg,
313+
&((wc_Sha384*)info->free.obj)->used,
314+
&((wc_Sha384*)info->free.obj)->len,
315+
((wc_Sha384*)info->free.obj)->heap);
316+
return 0;
317+
#endif
318+
#ifdef WOLFSSL_SHA512
319+
case WC_HASH_TYPE_SHA512:
320+
wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha512),
321+
&((wc_Sha512*)info->free.obj)->msg,
322+
&((wc_Sha512*)info->free.obj)->used,
323+
&((wc_Sha512*)info->free.obj)->len,
324+
((wc_Sha512*)info->free.obj)->heap);
325+
return 0;
326+
#endif
327+
#endif /* MAX3266X_SHA_CB */
328+
default:
329+
return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
330+
}
331+
}
332+
#endif /* WOLF_CRYPTO_CB_FREE */
333+
207334
/* General Callback Function to determine ALGO Type */
208335
int wc_MxcCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
209336
{
@@ -230,6 +357,18 @@ int wc_MxcCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
230357
ret = wc_MxcShaCryptoCb(info); /* Determine SHA HW or SW */
231358
break;
232359
#endif /* MAX3266X_SHA_CB */
360+
#ifdef WOLF_CRYPTO_CB_COPY
361+
case WC_ALGO_TYPE_COPY:
362+
MAX3266X_MSG("Using MXC Copy Callback:");
363+
ret = wc_MxcCopyCb(info);
364+
break;
365+
#endif /* WOLF_CRYPTO_CB_COPY */
366+
#ifdef WOLF_CRYPTO_CB_FREE
367+
case WC_ALGO_TYPE_FREE:
368+
MAX3266X_MSG("Using MXC Free Callback:");
369+
ret = wc_MxcFreeCb(info);
370+
break;
371+
#endif /* WOLF_CRYPTO_CB_FREE */
233372
default:
234373
MAX3266X_MSG("Callback not support with MXC, using SW");
235374
/* return this to bypass HW and use SW */
@@ -651,6 +790,50 @@ void wc_MXC_TPU_SHA_Free(byte** msg, word32* used, word32* len, void* heap)
651790
}
652791
}
653792

793+
/* Free HASH_KEEP message buffer and zero the full SHA context struct */
794+
void wc_MXC_TPU_SHA_FreeCtx(void* ctx, word32 ctxSz, byte** msg, word32* used,
795+
word32* len, void* heap)
796+
{
797+
if (ctx == NULL) {
798+
return;
799+
}
800+
wc_MXC_TPU_SHA_Free(msg, used, len, heap);
801+
XMEMSET(ctx, 0, ctxSz);
802+
}
803+
804+
/* Copy SHA context struct and deep copy the HASH_KEEP message buffer.
805+
* Frees any existing dst msg buffer to prevent memory leaks when copying
806+
* into an already-used context. */
807+
int wc_MXC_TPU_SHA_Copy(void* src, void* dst, word32 ctxSz,
808+
byte** dstMsg, word32* dstUsed, word32* dstLen,
809+
void* dstHeap, void* srcHeap)
810+
{
811+
byte* srcBuf;
812+
813+
if (src == NULL || dst == NULL || dstMsg == NULL ||
814+
dstUsed == NULL || dstLen == NULL || ctxSz == 0) {
815+
return BAD_FUNC_ARG;
816+
}
817+
818+
/* Free existing dst msg buffer using dst's original heap */
819+
wc_MXC_TPU_SHA_Free(dstMsg, dstUsed, dstLen, dstHeap);
820+
821+
/* Shallow copy the full context struct */
822+
XMEMCPY(dst, src, ctxSz);
823+
824+
/* Deep copy src msg buffer if present, allocate using src's heap */
825+
if (*dstMsg != NULL) {
826+
srcBuf = *dstMsg;
827+
*dstMsg = (byte*)XMALLOC(*dstLen, srcHeap, DYNAMIC_TYPE_TMP_BUFFER);
828+
if (*dstMsg == NULL) {
829+
return MEMORY_E;
830+
}
831+
XMEMCPY(*dstMsg, srcBuf, *dstUsed);
832+
}
833+
834+
return 0;
835+
}
836+
654837
/* Compute hash, free message buffer, and reset HASH_KEEP fields */
655838
int wc_MXC_TPU_SHA_Final(unsigned char** msg, unsigned int* used,
656839
unsigned int* len, void* heap,
@@ -713,24 +896,18 @@ WOLFSSL_API int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
713896
if (src == NULL || dst == NULL) {
714897
return BAD_FUNC_ARG;
715898
}
716-
XMEMCPY(dst, src, sizeof(*src));
717-
if (src->msg != NULL) {
718-
dst->msg = (byte*)XMALLOC(src->len, dst->heap,
719-
DYNAMIC_TYPE_TMP_BUFFER);
720-
if (dst->msg == NULL) {
721-
return MEMORY_E;
722-
}
723-
XMEMCPY(dst->msg, src->msg, src->used);
724-
}
725-
return 0;
899+
return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha),
900+
&dst->msg, &dst->used, &dst->len,
901+
dst->heap, src->heap);
726902
}
727903

728904
WOLFSSL_API void wc_ShaFree(wc_Sha* sha)
729905
{
730906
if (sha == NULL) {
731907
return;
732908
}
733-
wc_MXC_TPU_SHA_Free(&sha->msg, &sha->used, &sha->len, sha->heap);
909+
wc_MXC_TPU_SHA_FreeCtx(sha, sizeof(wc_Sha), &sha->msg, &sha->used,
910+
&sha->len, sha->heap);
734911
}
735912

736913
#endif /* NO_SHA */
@@ -788,24 +965,18 @@ WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst)
788965
if (src == NULL || dst == NULL) {
789966
return BAD_FUNC_ARG;
790967
}
791-
XMEMCPY(dst, src, sizeof(*src));
792-
if (src->msg != NULL) {
793-
dst->msg = (byte*)XMALLOC(src->len, dst->heap,
794-
DYNAMIC_TYPE_TMP_BUFFER);
795-
if (dst->msg == NULL) {
796-
return MEMORY_E;
797-
}
798-
XMEMCPY(dst->msg, src->msg, src->used);
799-
}
800-
return 0;
968+
return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha224),
969+
&dst->msg, &dst->used, &dst->len,
970+
dst->heap, src->heap);
801971
}
802972

803973
WOLFSSL_API void wc_Sha224Free(wc_Sha224* sha224)
804974
{
805975
if (sha224 == NULL) {
806976
return;
807977
}
808-
wc_MXC_TPU_SHA_Free(&sha224->msg, &sha224->used, &sha224->len,
978+
wc_MXC_TPU_SHA_FreeCtx(sha224, sizeof(wc_Sha224), &sha224->msg,
979+
&sha224->used, &sha224->len,
809980
sha224->heap);
810981
}
811982

@@ -864,24 +1035,18 @@ WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
8641035
if (src == NULL || dst == NULL) {
8651036
return BAD_FUNC_ARG;
8661037
}
867-
XMEMCPY(dst, src, sizeof(*src));
868-
if (src->msg != NULL) {
869-
dst->msg = (byte*)XMALLOC(src->len, dst->heap,
870-
DYNAMIC_TYPE_TMP_BUFFER);
871-
if (dst->msg == NULL) {
872-
return MEMORY_E;
873-
}
874-
XMEMCPY(dst->msg, src->msg, src->used);
875-
}
876-
return 0;
1038+
return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha256),
1039+
&dst->msg, &dst->used, &dst->len,
1040+
dst->heap, src->heap);
8771041
}
8781042

8791043
WOLFSSL_API void wc_Sha256Free(wc_Sha256* sha256)
8801044
{
8811045
if (sha256 == NULL) {
8821046
return;
8831047
}
884-
wc_MXC_TPU_SHA_Free(&sha256->msg, &sha256->used, &sha256->len,
1048+
wc_MXC_TPU_SHA_FreeCtx(sha256, sizeof(wc_Sha256), &sha256->msg,
1049+
&sha256->used, &sha256->len,
8851050
sha256->heap);
8861051
}
8871052

@@ -940,24 +1105,18 @@ WOLFSSL_API int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
9401105
if (src == NULL || dst == NULL) {
9411106
return BAD_FUNC_ARG;
9421107
}
943-
XMEMCPY(dst, src, sizeof(*src));
944-
if (src->msg != NULL) {
945-
dst->msg = (byte*)XMALLOC(src->len, dst->heap,
946-
DYNAMIC_TYPE_TMP_BUFFER);
947-
if (dst->msg == NULL) {
948-
return MEMORY_E;
949-
}
950-
XMEMCPY(dst->msg, src->msg, src->used);
951-
}
952-
return 0;
1108+
return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha384),
1109+
&dst->msg, &dst->used, &dst->len,
1110+
dst->heap, src->heap);
9531111
}
9541112

9551113
WOLFSSL_API void wc_Sha384Free(wc_Sha384* sha384)
9561114
{
9571115
if (sha384 == NULL) {
9581116
return;
9591117
}
960-
wc_MXC_TPU_SHA_Free(&sha384->msg, &sha384->used, &sha384->len,
1118+
wc_MXC_TPU_SHA_FreeCtx(sha384, sizeof(wc_Sha384), &sha384->msg,
1119+
&sha384->used, &sha384->len,
9611120
sha384->heap);
9621121
}
9631122

@@ -1016,24 +1175,18 @@ WOLFSSL_API int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
10161175
if (src == NULL || dst == NULL) {
10171176
return BAD_FUNC_ARG;
10181177
}
1019-
XMEMCPY(dst, src, sizeof(*src));
1020-
if (src->msg != NULL) {
1021-
dst->msg = (byte*)XMALLOC(src->len, dst->heap,
1022-
DYNAMIC_TYPE_TMP_BUFFER);
1023-
if (dst->msg == NULL) {
1024-
return MEMORY_E;
1025-
}
1026-
XMEMCPY(dst->msg, src->msg, src->used);
1027-
}
1028-
return 0;
1178+
return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha512),
1179+
&dst->msg, &dst->used, &dst->len,
1180+
dst->heap, src->heap);
10291181
}
10301182

10311183
WOLFSSL_API void wc_Sha512Free(wc_Sha512* sha512)
10321184
{
10331185
if (sha512 == NULL) {
10341186
return;
10351187
}
1036-
wc_MXC_TPU_SHA_Free(&sha512->msg, &sha512->used, &sha512->len,
1188+
wc_MXC_TPU_SHA_FreeCtx(sha512, sizeof(wc_Sha512), &sha512->msg,
1189+
&sha512->used, &sha512->len,
10371190
sha512->heap);
10381191
}
10391192

0 commit comments

Comments
 (0)