Skip to content

Commit 6892bcb

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 ee3c785 commit 6892bcb

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
@@ -1195,12 +1198,6 @@ int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
11951198
esp_sha_ctx_copy(src, dst);
11961199
#endif
11971200

1198-
#ifdef MAX3266X_SHA_CB
1199-
ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx));
1200-
if (ret != 0) {
1201-
return ret;
1202-
}
1203-
#endif
12041201

12051202
#if defined(PSOC6_HASH_SHA1)
12061203
wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA1, 0);
@@ -1209,6 +1206,18 @@ int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
12091206
#ifdef WOLFSSL_HASH_FLAGS
12101207
dst->flags |= WC_HASH_FLAG_ISCOPY;
12111208
#endif
1209+
1210+
#if defined(WOLFSSL_HASH_KEEP)
1211+
if (src->msg != NULL) {
1212+
dst->msg = (byte*)XMALLOC(src->len, dst->heap,
1213+
DYNAMIC_TYPE_TMP_BUFFER);
1214+
if (dst->msg == NULL) {
1215+
return MEMORY_E;
1216+
}
1217+
XMEMCPY(dst->msg, src->msg, src->used);
1218+
}
1219+
#endif
1220+
12121221
return ret;
12131222
}
12141223
#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);
@@ -2752,12 +2731,6 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
27522731
wc_MAXQ10XX_Sha256Copy(src);
27532732
#endif
27542733

2755-
#ifdef MAX3266X_SHA_CB
2756-
ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx));
2757-
if (ret != 0) {
2758-
return ret;
2759-
}
2760-
#endif
27612734

27622735
#ifdef WOLFSSL_SMALL_STACK_CACHE
27632736
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
}
@@ -2310,12 +2295,6 @@ int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
23102295
}
23112296
#endif
23122297

2313-
#ifdef MAX3266X_SHA_CB
2314-
ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx));
2315-
if (ret != 0) {
2316-
return ret;
2317-
}
2318-
#endif
23192298

23202299
#if defined(PSOC6_HASH_SHA2)
23212300
wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA512, 0);
@@ -2750,12 +2729,6 @@ int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
27502729
}
27512730
#endif
27522731

2753-
#ifdef MAX3266X_SHA_CB
2754-
ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx));
2755-
if (ret != 0) {
2756-
return ret;
2757-
}
2758-
#endif
27592732

27602733
#if defined(PSOC6_HASH_SHA2)
27612734
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
@@ -215,9 +215,6 @@ struct wc_Sha256 {
215215
#ifdef WOLFSSL_DEVCRYPTO_HASH
216216
WC_CRYPTODEV ctx;
217217
#endif
218-
#if defined(MAX3266X_SHA_CB) || defined(MAX3266X_SHA)
219-
wc_MXC_Sha mxcCtx;
220-
#endif
221218
#if defined(WOLFSSL_DEVCRYPTO_HASH) || defined(WOLFSSL_HASH_KEEP)
222219
byte* msg;
223220
word32 used;

wolfssl/wolfcrypt/sha512.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,6 @@ struct wc_Sha512 {
195195
int devId;
196196
void* devCtx; /* generic crypto callback context */
197197
#endif
198-
#if defined(MAX3266X_SHA_CB) || defined(MAX3266X_SHA)
199-
wc_MXC_Sha mxcCtx;
200-
#endif
201198
#ifdef WOLFSSL_HASH_FLAGS
202199
word32 flags; /* enum wc_HashFlags in hash.h */
203200
#endif

0 commit comments

Comments
 (0)