HashFree for TI hash memory leak

This commit is contained in:
Takashi Kojo
2015-08-15 16:51:23 +09:00
parent e6ab7de923
commit 6376736129
4 changed files with 51 additions and 13 deletions

View File

@ -5940,13 +5940,12 @@ static INLINE void Md5Rounds(int rounds, const byte* data, int sz)
{ {
Md5 md5; Md5 md5;
int i; int i;
byte dummy[MD5_DIGEST_SIZE] ;
wc_InitMd5(&md5); wc_InitMd5(&md5);
for (i = 0; i < rounds; i++) for (i = 0; i < rounds; i++)
wc_Md5Update(&md5, data, sz); wc_Md5Update(&md5, data, sz);
wc_Md5Final(&md5, dummy) ; /* in case needed to release resources */ wc_Md5Free(&md5) ; /* in case needed to release resources */
} }
@ -5956,13 +5955,12 @@ static INLINE void ShaRounds(int rounds, const byte* data, int sz)
{ {
Sha sha; Sha sha;
int i; int i;
byte dummy[SHA_DIGEST_SIZE] ;
wc_InitSha(&sha); /* no error check on purpose, dummy round */ wc_InitSha(&sha); /* no error check on purpose, dummy round */
for (i = 0; i < rounds; i++) for (i = 0; i < rounds; i++)
wc_ShaUpdate(&sha, data, sz); wc_ShaUpdate(&sha, data, sz);
wc_ShaFinal(&sha, dummy) ; /* in case needed to release resources */ wc_ShaFree(&sha) ; /* in case needed to release resources */
} }
#endif #endif
@ -5973,7 +5971,6 @@ static INLINE void Sha256Rounds(int rounds, const byte* data, int sz)
{ {
Sha256 sha256; Sha256 sha256;
int i; int i;
byte dummy[SHA256_DIGEST_SIZE] ;
wc_InitSha256(&sha256); /* no error check on purpose, dummy round */ wc_InitSha256(&sha256); /* no error check on purpose, dummy round */
@ -5981,7 +5978,7 @@ static INLINE void Sha256Rounds(int rounds, const byte* data, int sz)
wc_Sha256Update(&sha256, data, sz); wc_Sha256Update(&sha256, data, sz);
/* no error check on purpose, dummy round */ /* no error check on purpose, dummy round */
} }
wc_Sha256Final(&sha256, dummy) ; /* in case needed to release resources */ wc_Sha256Free(&sha256) ; /* in case needed to release resources */
} }
#endif #endif
@ -5993,7 +5990,6 @@ static INLINE void Sha384Rounds(int rounds, const byte* data, int sz)
{ {
Sha384 sha384; Sha384 sha384;
int i; int i;
byte dummy[SHA384_DIGEST_SIZE] ;
wc_InitSha384(&sha384); /* no error check on purpose, dummy round */ wc_InitSha384(&sha384); /* no error check on purpose, dummy round */
@ -6001,7 +5997,7 @@ static INLINE void Sha384Rounds(int rounds, const byte* data, int sz)
wc_Sha384Update(&sha384, data, sz); wc_Sha384Update(&sha384, data, sz);
/* no error check on purpose, dummy round */ /* no error check on purpose, dummy round */
} }
wc_Sha384Final(&sha384, dummy) ; /* in case needed to release resources */ wc_Sha384Free(&sha384) ; /* in case needed to release resources */
} }
#endif #endif
@ -6013,7 +6009,6 @@ static INLINE void Sha512Rounds(int rounds, const byte* data, int sz)
{ {
Sha512 sha512; Sha512 sha512;
int i; int i;
byte dummy[SHA512_DIGEST_SIZE] ;
wc_InitSha512(&sha512); /* no error check on purpose, dummy round */ wc_InitSha512(&sha512); /* no error check on purpose, dummy round */
@ -6021,7 +6016,7 @@ static INLINE void Sha512Rounds(int rounds, const byte* data, int sz)
wc_Sha512Update(&sha512, data, sz); wc_Sha512Update(&sha512, data, sz);
/* no error check on purpose, dummy round */ /* no error check on purpose, dummy round */
} }
wc_Sha512Final(&sha512, dummy) ; /* in case needed to release resources */ wc_Sha512Free(&sha512) ; /* in case needed to release resources */
} }
#endif #endif

16
wolfcrypt/src/hash.c Normal file → Executable file
View File

@ -42,6 +42,9 @@ void wc_Md5GetHash(Md5* md5, byte* hash)
WOLFSSL_API void wc_Md5RestorePos(Md5* m1, Md5* m2) { WOLFSSL_API void wc_Md5RestorePos(Md5* m1, Md5* m2) {
*m1 = *m2 ; *m1 = *m2 ;
} }
WOLFSSL_API void wc_Md5Free(Md5* md5) { (void) md5 ; }
#endif #endif
#if !defined(NO_SHA) #if !defined(NO_SHA)
@ -89,6 +92,8 @@ int wc_ShaHash(const byte* data, word32 len, byte* hash)
} }
WOLFSSL_API void wc_ShaFree(Sha* sha) { (void) sha ; }
#endif /* !defined(NO_SHA) */ #endif /* !defined(NO_SHA) */
#if !defined(NO_SHA256) #if !defined(NO_SHA256)
@ -136,8 +141,12 @@ int wc_Sha256Hash(const byte* data, word32 len, byte* hash)
return ret; return ret;
} }
WOLFSSL_API void wc_Sha256Free(Sha256* sha256) { (void)sha256 ; }
#endif /* !defined(NO_SHA256) */ #endif /* !defined(NO_SHA256) */
#endif /* !defined(WOLFSSL_TI_HASH) */
#if defined(WOLFSSL_SHA512) #if defined(WOLFSSL_SHA512)
int wc_Sha512Hash(const byte* data, word32 len, byte* hash) int wc_Sha512Hash(const byte* data, word32 len, byte* hash)
@ -172,6 +181,8 @@ int wc_Sha512Hash(const byte* data, word32 len, byte* hash)
return ret; return ret;
} }
WOLFSSL_API void wc_Sha512Free(Sha512* sha512) { (void)sha512 ; }
#if defined(WOLFSSL_SHA384) #if defined(WOLFSSL_SHA384)
int wc_Sha384Hash(const byte* data, word32 len, byte* hash) int wc_Sha384Hash(const byte* data, word32 len, byte* hash)
{ {
@ -205,8 +216,7 @@ int wc_Sha384Hash(const byte* data, word32 len, byte* hash)
return ret; return ret;
} }
WOLFSSL_API void wc_Sha384Free(Sha384* sha384) { (void) sha384 ; }
#endif /* defined(WOLFSSL_SHA384) */ #endif /* defined(WOLFSSL_SHA384) */
#endif /* defined(WOLFSSL_SHA512) */ #endif /* defined(WOLFSSL_SHA512) */
#endif /* !defined(WOLFSSL_TI_HASH) */

28
wolfcrypt/src/port/ti/ti-hash.c Normal file → Executable file
View File

@ -151,6 +151,13 @@ static int hashHash(const byte* data, word32 len, byte* hash, word32 algo, word3
return ret; return ret;
} }
static int hashFree(wolfssl_TI_Hash *hash)
{
XFREE(hash->msg, NULL, DYNAMIC_TYPE_TMP_BUFFER);
hashInit(hash) ;
return 0 ;
}
#if !defined(NO_MD5) #if !defined(NO_MD5)
WOLFSSL_API void wc_InitMd5(Md5* md5) WOLFSSL_API void wc_InitMd5(Md5* md5)
{ {
@ -183,6 +190,11 @@ WOLFSSL_API int wc_Md5Hash(const byte*data, word32 len, byte*hash)
return hashHash(data, len, hash, SHAMD5_ALGO_MD5, MD5_DIGEST_SIZE) ; return hashHash(data, len, hash, SHAMD5_ALGO_MD5, MD5_DIGEST_SIZE) ;
} }
WOLFSSL_API void wc_Md5Free(Md5* md5)
{
hashFree((wolfssl_TI_Hash *)md5) ;
}
#endif /* NO_MD5 */ #endif /* NO_MD5 */
#if !defined(NO_SHA) #if !defined(NO_SHA)
@ -217,6 +229,11 @@ WOLFSSL_API int wc_ShaHash(const byte*data, word32 len, byte*hash)
return hashHash(data, len, hash, SHAMD5_ALGO_SHA1, SHA_DIGEST_SIZE) ; return hashHash(data, len, hash, SHAMD5_ALGO_SHA1, SHA_DIGEST_SIZE) ;
} }
WOLFSSL_API void wc_ShaFree(Sha* sha)
{
hashFree((wolfssl_TI_Hash *)sha) ;
}
#endif /* NO_SHA */ #endif /* NO_SHA */
#if defined(HAVE_SHA224) #if defined(HAVE_SHA224)
@ -251,6 +268,11 @@ WOLFSSL_API int wc_Sha224Hash(const byte* data, word32 len, byte*hash)
return hashHash(data, len, hash, SHAMD5_ALGO_SHA224, SHA224_DIGEST_SIZE) ; return hashHash(data, len, hash, SHAMD5_ALGO_SHA224, SHA224_DIGEST_SIZE) ;
} }
WOLFSSL_API void wc_Sha224Free(Sha224* sha224)
{
hashFree((wolfssl_TI_Hash *)sha224) ;
}
#endif /* HAVE_SHA224 */ #endif /* HAVE_SHA224 */
#if !defined(NO_SHA256) #if !defined(NO_SHA256)
@ -284,6 +306,12 @@ WOLFSSL_API int wc_Sha256Hash(const byte* data, word32 len, byte*hash)
{ {
return hashHash(data, len, hash, SHAMD5_ALGO_SHA256, SHA256_DIGEST_SIZE) ; return hashHash(data, len, hash, SHAMD5_ALGO_SHA256, SHA256_DIGEST_SIZE) ;
} }
WOLFSSL_API void wc_Sha256Free(Sha256* sha256)
{
hashFree((wolfssl_TI_Hash *)sha256) ;
}
#endif #endif
#endif #endif

5
wolfssl/wolfcrypt/hash.h Normal file → Executable file
View File

@ -28,6 +28,7 @@
#include <wolfssl/wolfcrypt/md5.h> #include <wolfssl/wolfcrypt/md5.h>
WOLFSSL_API void wc_Md5GetHash(Md5*, byte*); WOLFSSL_API void wc_Md5GetHash(Md5*, byte*);
WOLFSSL_API void wc_Md5RestorePos(Md5*, Md5*) ; WOLFSSL_API void wc_Md5RestorePos(Md5*, Md5*) ;
WOLFSSL_API void wc_Md5Free(Md5*);
#endif #endif
#ifndef NO_SHA #ifndef NO_SHA
@ -35,6 +36,7 @@ WOLFSSL_API void wc_Md5RestorePos(Md5*, Md5*) ;
WOLFSSL_API int wc_ShaGetHash(Sha*, byte*); WOLFSSL_API int wc_ShaGetHash(Sha*, byte*);
WOLFSSL_API void wc_ShaRestorePos(Sha*, Sha*) ; WOLFSSL_API void wc_ShaRestorePos(Sha*, Sha*) ;
WOLFSSL_API int wc_ShaHash(const byte*, word32, byte*); WOLFSSL_API int wc_ShaHash(const byte*, word32, byte*);
WOLFSSL_API void wc_ShaFree(Sha*);
#endif #endif
#ifndef NO_SHA256 #ifndef NO_SHA256
@ -42,13 +44,16 @@ WOLFSSL_API int wc_ShaHash(const byte*, word32, byte*);
WOLFSSL_API int wc_Sha256GetHash(Sha256*, byte*); WOLFSSL_API int wc_Sha256GetHash(Sha256*, byte*);
WOLFSSL_API void wc_Sha256RestorePos(Sha256*, Sha256*) ; WOLFSSL_API void wc_Sha256RestorePos(Sha256*, Sha256*) ;
WOLFSSL_API int wc_Sha256Hash(const byte*, word32, byte*); WOLFSSL_API int wc_Sha256Hash(const byte*, word32, byte*);
WOLFSSL_API void wc_Sha256Free(Sha256*);
#endif #endif
#ifdef WOLFSSL_SHA512 #ifdef WOLFSSL_SHA512
#include <wolfssl/wolfcrypt/sha512.h> #include <wolfssl/wolfcrypt/sha512.h>
WOLFSSL_API int wc_Sha512Hash(const byte*, word32, byte*); WOLFSSL_API int wc_Sha512Hash(const byte*, word32, byte*);
WOLFSSL_API void wc_Sha512Free(Sha512*);
#if defined(WOLFSSL_SHA384) #if defined(WOLFSSL_SHA384)
WOLFSSL_API int wc_Sha384Hash(const byte*, word32, byte*); WOLFSSL_API int wc_Sha384Hash(const byte*, word32, byte*);
WOLFSSL_API void wc_Sha384Free(Sha384*);
#endif /* defined(WOLFSSL_SHA384) */ #endif /* defined(WOLFSSL_SHA384) */
#endif /* WOLFSSL_SHA512 */ #endif /* WOLFSSL_SHA512 */