Skip to content

Commit c3b329e

Browse files
committed
Refactor to use HASH_KEEP option instead of dedicated context for SHA, also add HASH_KEEP to sha1 context with correct init/free calls
1 parent 2f2fca6 commit c3b329e

9 files changed

Lines changed: 334 additions & 535 deletions

File tree

wolfcrypt/src/port/maxim/max3266x.c

Lines changed: 288 additions & 430 deletions
Large diffs are not rendered by default.

wolfcrypt/src/sha.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,10 @@ int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
561561
sha->devId = devId;
562562
sha->devCtx = NULL;
563563
#endif
564-
565-
#ifdef MAX3266X_SHA_CB
566-
ret = wc_MXC_TPU_SHA_Init(&(sha->mxcCtx));
567-
if (ret != 0) {
568-
return ret;
569-
}
564+
#ifdef WOLFSSL_HASH_KEEP
565+
sha->msg = NULL;
566+
sha->len = 0;
567+
sha->used = 0;
570568
#endif
571569

572570
#ifdef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
@@ -1087,9 +1085,6 @@ void wc_ShaFree(wc_Sha* sha)
10871085
#ifdef WOLFSSL_PIC32MZ_HASH
10881086
wc_ShaPic32Free(sha);
10891087
#endif
1090-
#ifdef MAX3266X_SHA_CB
1091-
wc_MXC_TPU_SHA_Free(&(sha->mxcCtx));
1092-
#endif
10931088
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
10941089
se050_hash_free(&sha->se050Ctx);
10951090
#endif
@@ -1105,6 +1100,14 @@ void wc_ShaFree(wc_Sha* sha)
11051100
DCPShaFree(sha);
11061101
#endif
11071102

1103+
#ifdef WOLFSSL_HASH_KEEP
1104+
if (sha->msg != NULL) {
1105+
ForceZero(sha->msg, sha->len);
1106+
XFREE(sha->msg, sha->heap, DYNAMIC_TYPE_TMP_BUFFER);
1107+
sha->msg = NULL;
1108+
}
1109+
#endif
1110+
11081111
#if defined(PSOC6_HASH_SHA1)
11091112
wc_Psoc6_Sha_Free();
11101113
#endif
@@ -1198,12 +1201,6 @@ int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
11981201
esp_sha_ctx_copy(src, dst);
11991202
#endif
12001203

1201-
#ifdef MAX3266X_SHA_CB
1202-
ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx));
1203-
if (ret != 0) {
1204-
return ret;
1205-
}
1206-
#endif
12071204

12081205
#if defined(PSOC6_HASH_SHA1)
12091206
wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA1, 0);
@@ -1212,6 +1209,18 @@ int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
12121209
#ifdef WOLFSSL_HASH_FLAGS
12131210
dst->flags |= WC_HASH_FLAG_ISCOPY;
12141211
#endif
1212+
1213+
#if defined(WOLFSSL_HASH_KEEP)
1214+
if (src->msg != NULL) {
1215+
dst->msg = (byte*)XMALLOC(src->len, dst->heap,
1216+
DYNAMIC_TYPE_TMP_BUFFER);
1217+
if (dst->msg == NULL) {
1218+
return MEMORY_E;
1219+
}
1220+
XMEMCPY(dst->msg, src->msg, src->used);
1221+
}
1222+
#endif
1223+
12151224
return ret;
12161225
}
12171226
#endif /* WOLFSSL_RENESAS_RX64_HASH */

wolfcrypt/src/sha256.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,12 +1113,6 @@ int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
11131113
(void)devId;
11141114
#endif
11151115

1116-
#ifdef MAX3266X_SHA_CB
1117-
ret = wc_MXC_TPU_SHA_Init(&(sha256->mxcCtx));
1118-
if (ret != 0) {
1119-
return ret;
1120-
}
1121-
#endif
11221116

11231117
#ifdef WOLFSSL_SMALL_STACK_CACHE
11241118
sha256->W = NULL;
@@ -1171,12 +1165,6 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
11711165
sha256->devId = devId;
11721166
sha256->devCtx = NULL;
11731167
#endif
1174-
#ifdef MAX3266X_SHA_CB
1175-
ret = wc_MXC_TPU_SHA_Init(&(sha256->mxcCtx));
1176-
if (ret != 0) {
1177-
return ret;
1178-
}
1179-
#endif
11801168
#ifdef WOLFSSL_SMALL_STACK_CACHE
11811169
sha256->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
11821170
sha256->heap, DYNAMIC_TYPE_DIGEST);
@@ -2150,12 +2138,6 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
21502138
sha224->devId = devId;
21512139
sha224->devCtx = NULL;
21522140
#endif
2153-
#ifdef MAX3266X_SHA_CB
2154-
ret = wc_MXC_TPU_SHA_Init(&(sha224->mxcCtx));
2155-
if (ret != 0) {
2156-
return ret;
2157-
}
2158-
#endif
21592141
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
21602142
#if defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224)
21612143
/* We know this is a fresh, uninitialized item, so set to INIT */
@@ -2428,9 +2410,6 @@ void wc_Sha256Free(wc_Sha256* sha256)
24282410
}
24292411
#endif
24302412

2431-
#ifdef MAX3266X_SHA_CB
2432-
wc_MXC_TPU_SHA_Free(&(sha256->mxcCtx));
2433-
#endif
24342413

24352414
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
24362415
wolfAsync_DevCtxFree(&sha256->asyncDev, WOLFSSL_ASYNC_MARKER_SHA256);
@@ -2758,12 +2737,6 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
27582737
wc_MAXQ10XX_Sha256Copy(src);
27592738
#endif
27602739

2761-
#ifdef MAX3266X_SHA_CB
2762-
ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx));
2763-
if (ret != 0) {
2764-
return ret;
2765-
}
2766-
#endif
27672740

27682741
#ifdef WOLFSSL_SMALL_STACK_CACHE
27692742
dst->W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,

wolfcrypt/src/sha512.c

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,8 @@ static int InitSha512_Family(wc_Sha512* sha512, void* heap, int devId,
887887

888888
#ifdef WOLFSSL_HASH_KEEP
889889
sha512->msg = NULL;
890+
sha512->len = 0;
891+
sha512->used = 0;
890892
#endif
891893

892894
/* call the initialization function pointed to by initfp */
@@ -927,11 +929,6 @@ int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
927929
sha512->ctx.mode = ESP32_SHA_INIT;
928930
#endif
929931

930-
#ifdef MAX3266X_SHA_CB
931-
if (wc_MXC_TPU_SHA_Init(&(sha512->mxcCtx)) != 0){
932-
return BAD_FUNC_ARG;
933-
}
934-
#endif
935932

936933
return InitSha512_Family(sha512, heap, devId, InitSha512);
937934
}
@@ -1676,9 +1673,6 @@ void wc_Sha512Free(wc_Sha512* sha512)
16761673
}
16771674
#endif
16781675

1679-
#ifdef MAX3266X_SHA_CB
1680-
wc_MXC_TPU_SHA_Free(&(sha512->mxcCtx));
1681-
#endif
16821676

16831677
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
16841678
wolfAsync_DevCtxFree(&sha512->asyncDev, WOLFSSL_ASYNC_MARKER_SHA512);
@@ -2062,12 +2056,6 @@ int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
20622056
sha384->ctx.mode = ESP32_SHA_INIT;
20632057
#endif
20642058

2065-
#ifdef MAX3266X_SHA_CB
2066-
ret = wc_MXC_TPU_SHA_Init(&(sha384->mxcCtx));
2067-
if (ret != 0) {
2068-
return ret;
2069-
}
2070-
#endif
20712059

20722060
ret = InitSha384(sha384);
20732061
if (ret != 0) {
@@ -2172,9 +2160,6 @@ void wc_Sha384Free(wc_Sha384* sha384)
21722160
}
21732161
#endif
21742162

2175-
#ifdef MAX3266X_SHA_CB
2176-
wc_MXC_TPU_SHA_Free(&(sha384->mxcCtx));
2177-
#endif
21782163

21792164
ForceZero(sha384, sizeof(*sha384));
21802165
}
@@ -2313,12 +2298,6 @@ int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
23132298
}
23142299
#endif
23152300

2316-
#ifdef MAX3266X_SHA_CB
2317-
ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx));
2318-
if (ret != 0) {
2319-
return ret;
2320-
}
2321-
#endif
23222301

23232302
#if defined(PSOC6_HASH_SHA2)
23242303
wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA512, 0);
@@ -2756,12 +2735,6 @@ int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
27562735
}
27572736
#endif
27582737

2759-
#ifdef MAX3266X_SHA_CB
2760-
ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx));
2761-
if (ret != 0) {
2762-
return ret;
2763-
}
2764-
#endif
27652738

27662739
#if defined(PSOC6_HASH_SHA2)
27672740
wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA384, 0);

wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@
5959
#endif /* HAVE_AES_DECRYPT */
6060

6161

62-
WOLFSSL_LOCAL int wc_MXC_Sha256Update(wc_MXC_Sha* sha256,
63-
const unsigned char* data,
64-
unsigned int len);
65-
WOLFSSL_LOCAL int wc_MXC_Sha256Final(wc_MXC_Sha* sha256,
66-
unsigned char* hash);
67-
6862
#ifdef __cplusplus
6963
} /* extern "C" */
7064
#endif

wolfssl/wolfcrypt/port/maxim/max3266x.h

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
/* Some extra conditions when using callbacks */
3434
#if defined(WOLF_CRYPTO_CB)
3535
#define MAX3266X_CB
36-
#define WOLF_CRYPTO_CB_COPY /* Enable copy callback for deep copy */
37-
#define WOLF_CRYPTO_CB_FREE /* Enable free callback for proper cleanup */
3836
#ifdef MAX3266X_MATH
3937
#error Cannot have MAX3266X_MATH and MAX3266X_CB
4038
#endif
@@ -238,14 +236,10 @@
238236

239237
#if defined(MAX3266X_SHA) || defined(MAX3266X_SHA_CB)
240238

241-
/* Need to update this struct accordingly if other SHA Structs change */
242-
/* This is a generic struct to use so only this is needed */
243-
244-
typedef struct {
245-
unsigned char *msg;
246-
unsigned int used;
247-
unsigned int size;
248-
} wc_MXC_Sha;
239+
/* Use HASH_KEEP to accumulate message data for one-shot TPU hardware */
240+
#ifndef WOLFSSL_HASH_KEEP
241+
#define WOLFSSL_HASH_KEEP
242+
#endif
249243

250244
#if !defined(NO_SHA)
251245
/* Define the SHA digest for an empty string */
@@ -313,19 +307,26 @@
313307
#endif /* WOLFSSL_SHA512 */
314308

315309

316-
WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Init(wc_MXC_Sha *hash);
317-
WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Update(wc_MXC_Sha *hash,
318-
const unsigned char* data,
319-
unsigned int size);
320-
WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Final(wc_MXC_Sha *hash,
310+
/* Check for empty message and provide pre-computed digest if so */
311+
WOLFSSL_LOCAL int wc_MXC_TPU_SHA_GetDigest(const unsigned char* msg,
312+
unsigned int msgSz,
321313
unsigned char* digest,
322314
MXC_TPU_HASH_TYPE algo);
323-
WOLFSSL_LOCAL int wc_MXC_TPU_SHA_GetHash(wc_MXC_Sha *hash,
315+
/* Compute hash from accumulated message using TPU hardware */
316+
WOLFSSL_LOCAL int wc_MXC_TPU_SHA_GetHash(const unsigned char* msg,
317+
unsigned int msgSz,
324318
unsigned char* digest,
325319
MXC_TPU_HASH_TYPE algo);
326-
WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Copy(wc_MXC_Sha* src, wc_MXC_Sha* dst);
327-
WOLFSSL_LOCAL void wc_MXC_TPU_SHA_Free(wc_MXC_Sha* hash);
328-
WOLFSSL_LOCAL int wc_MXC_TPU_SHA_GetDigest(wc_MXC_Sha *hash,
320+
/* Free HASH_KEEP message buffer and reset fields */
321+
WOLFSSL_LOCAL void wc_MXC_TPU_SHA_Free(unsigned char** msg,
322+
unsigned int* used,
323+
unsigned int* len,
324+
void* heap);
325+
/* Compute hash, free message buffer, and reset fields */
326+
WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Final(unsigned char** msg,
327+
unsigned int* used,
328+
unsigned int* len,
329+
void* heap,
329330
unsigned char* digest,
330331
MXC_TPU_HASH_TYPE algo);
331332

wolfssl/wolfcrypt/sha.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,6 @@ struct wc_Sha {
175175
int devId;
176176
void* devCtx; /* generic crypto callback context */
177177
#endif
178-
#if defined(MAX3266X_SHA_CB) || defined(MAX3266X_SHA)
179-
wc_MXC_Sha mxcCtx;
180-
#endif
181178
#ifdef WOLFSSL_IMXRT1170_CAAM
182179
caam_hash_ctx_t ctx;
183180
caam_handle_t hndl;

wolfssl/wolfcrypt/sha256.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,6 @@ struct wc_Sha256 {
209209
#ifdef WOLFSSL_DEVCRYPTO_HASH
210210
WC_CRYPTODEV ctx;
211211
#endif
212-
#if defined(MAX3266X_SHA_CB) || defined(MAX3266X_SHA)
213-
wc_MXC_Sha mxcCtx;
214-
#endif
215212
#if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP)
216213
byte* msg;
217214
word32 used;

wolfssl/wolfcrypt/sha512.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,6 @@ struct wc_Sha512 {
189189
int devId;
190190
void* devCtx; /* generic crypto callback context */
191191
#endif
192-
#if defined(MAX3266X_SHA_CB) || defined(MAX3266X_SHA)
193-
wc_MXC_Sha mxcCtx;
194-
#endif
195192
#ifdef WOLFSSL_HASH_FLAGS
196193
word32 flags; /* enum wc_HashFlags in hash.h */
197194
#endif

0 commit comments

Comments
 (0)