@@ -686,7 +686,7 @@ int wc_Hash(enum wc_HashType hash_type, const byte* data,
686686 NULL , INVALID_DEVID );
687687}
688688
689- #ifndef WOLFSSL_NO_MALLOC
689+ #ifndef WC_NO_CONSTRUCTORS
690690wc_HashAlg * wc_HashNew (enum wc_HashType type , void * heap , int devId )
691691{
692692 wc_HashAlg * hash = (wc_HashAlg * )XMALLOC (sizeof (wc_HashAlg ), heap ,
@@ -702,7 +702,19 @@ wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId)
702702 }
703703 return hash ;
704704}
705- #endif
705+
706+ int wc_HashDelete (wc_HashAlg * * hash ) {
707+ int ret ;
708+ if ((hash == NULL ) || (* hash == NULL ))
709+ return BAD_FUNC_ARG ;
710+ ret = wc_HashFree (* hash , (* hash )-> type );
711+ if (ret < 0 )
712+ return ret ;
713+ XFREE (* hash , (* hash )-> heap , DYNAMIC_TYPE_HASHES );
714+ * hash = NULL ;
715+ return 0 ;
716+ }
717+ #endif /* !WC_NO_CONSTRUCTORS */
706718
707719int wc_HashInit_ex (wc_HashAlg * hash , enum wc_HashType type , void * heap ,
708720 int devId )
@@ -712,9 +724,14 @@ int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
712724 if (hash == NULL )
713725 return BAD_FUNC_ARG ;
714726
715- hash -> isAllocated = 0 ;
716727 hash -> type = type ;
717728
729+ #ifdef WC_NO_CONSTRUCTORS
730+ (void )heap ;
731+ #else
732+ hash -> heap = heap ;
733+ #endif
734+
718735 switch (type ) {
719736 case WC_HASH_TYPE_MD5 :
720737#ifndef NO_MD5
@@ -808,7 +825,6 @@ int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
808825 ret = BAD_FUNC_ARG ;
809826 };
810827
811- (void )heap ;
812828 (void )devId ;
813829
814830 return ret ;
@@ -1043,8 +1059,6 @@ int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out)
10431059int wc_HashFree (wc_HashAlg * hash , enum wc_HashType type )
10441060{
10451061 int ret = WC_NO_ERR_TRACE (HASH_TYPE_E ); /* Default to hash type error */
1046- void * heap = NULL ;
1047- byte isAllocated = 0 ;
10481062
10491063 if (hash == NULL )
10501064 return BAD_FUNC_ARG ;
@@ -1056,47 +1070,39 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
10561070 }
10571071#endif
10581072
1059- isAllocated = hash -> isAllocated ;
1060-
10611073 switch (type ) {
10621074 case WC_HASH_TYPE_MD5 :
10631075#ifndef NO_MD5
1064- heap = hash -> alg .md5 .heap ;
10651076 wc_Md5Free (& hash -> alg .md5 );
10661077 ret = 0 ;
10671078#endif
10681079 break ;
10691080 case WC_HASH_TYPE_SHA :
10701081#ifndef NO_SHA
1071- heap = hash -> alg .sha .heap ;
10721082 wc_ShaFree (& hash -> alg .sha );
10731083 ret = 0 ;
10741084#endif
10751085 break ;
10761086 case WC_HASH_TYPE_SHA224 :
10771087#ifdef WOLFSSL_SHA224
1078- heap = hash -> alg .sha224 .heap ;
10791088 wc_Sha224Free (& hash -> alg .sha224 );
10801089 ret = 0 ;
10811090#endif
10821091 break ;
10831092 case WC_HASH_TYPE_SHA256 :
10841093#ifndef NO_SHA256
1085- heap = hash -> alg .sha256 .heap ;
10861094 wc_Sha256Free (& hash -> alg .sha256 );
10871095 ret = 0 ;
10881096#endif
10891097 break ;
10901098 case WC_HASH_TYPE_SHA384 :
10911099#ifdef WOLFSSL_SHA384
1092- heap = hash -> alg .sha384 .heap ;
10931100 wc_Sha384Free (& hash -> alg .sha384 );
10941101 ret = 0 ;
10951102#endif
10961103 break ;
10971104 case WC_HASH_TYPE_SHA512 :
10981105#ifdef WOLFSSL_SHA512
1099- heap = hash -> alg .sha512 .heap ;
11001106 wc_Sha512Free (& hash -> alg .sha512 );
11011107 ret = 0 ;
11021108#endif
@@ -1123,7 +1129,6 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
11231129 #endif
11241130 case WC_HASH_TYPE_SHA3_224 :
11251131#if defined(WOLFSSL_SHA3 ) && !defined(WOLFSSL_NOSHA3_224 )
1126- heap = hash -> alg .sha3 .heap ;
11271132 wc_Sha3_224_Free (& hash -> alg .sha3 );
11281133 ret = 0 ;
11291134#endif
@@ -1149,7 +1154,6 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
11491154
11501155 #ifdef WOLFSSL_SM3
11511156 case WC_HASH_TYPE_SM3 :
1152- heap = hash -> alg .sm3 .heap ;
11531157 wc_Sm3Free (& hash -> alg .sm3 );
11541158 ret = 0 ;
11551159 break ;
@@ -1172,11 +1176,6 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
11721176 ret = BAD_FUNC_ARG ;
11731177 };
11741178
1175- if (isAllocated ) {
1176- XFREE (hash , heap , DYNAMIC_TYPE_HASHES );
1177- (void )heap ;
1178- }
1179-
11801179 return ret ;
11811180}
11821181
0 commit comments