From e9aae8b571da4a7df16f3de556bfd37bf5b45472 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Wed, 6 Apr 2022 15:08:19 -0600 Subject: [PATCH] use hash grow function with cmac --- wolfcrypt/src/cmac.c | 33 ++++----------------------------- wolfcrypt/src/hash.c | 2 +- wolfcrypt/src/sha256.c | 4 ++-- wolfcrypt/src/sha512.c | 4 ++-- wolfssl/wolfcrypt/hash.h | 2 +- 5 files changed, 10 insertions(+), 35 deletions(-) diff --git a/wolfcrypt/src/cmac.c b/wolfcrypt/src/cmac.c index 7328db5e0..d9bcd411c 100644 --- a/wolfcrypt/src/cmac.c +++ b/wolfcrypt/src/cmac.c @@ -28,6 +28,9 @@ #ifdef WOLFSSL_QNX_CAAM #include #endif +#if defined(WOLFSSL_HASH_KEEP) +#include +#endif #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) @@ -62,37 +65,9 @@ * data to be hashed at once. * returns 0 on success */ -static int _wc_CMAC_Grow(byte** msg, word32* used, word32* len, const byte* in, - int inSz, void* heap) -{ - if (*len < *used + inSz) { - if (*msg == NULL) { - *msg = (byte*)XMALLOC(*used + inSz, heap, DYNAMIC_TYPE_TMP_BUFFER); - } - else { - byte* pt = (byte*)XMALLOC(*used + inSz, heap, - DYNAMIC_TYPE_TMP_BUFFER); - if (pt == NULL) { - return MEMORY_E; - } - XMEMCPY(pt, *msg, *used); - XFREE(*msg, heap, DYNAMIC_TYPE_TMP_BUFFER); - *msg = pt; - } - if (*msg == NULL) { - return MEMORY_E; - } - *len = *used + inSz; - } - XMEMCPY(*msg + *used, in, inSz); - *used += inSz; - return 0; -} - - int wc_CMAC_Grow(Cmac* cmac, const byte* in, int inSz) { - return _wc_CMAC_Grow(&cmac->msg, &cmac->used, &cmac->len, in, inSz, NULL); + return _wc_Hash_Grow(&cmac->msg, &cmac->used, &cmac->len, in, inSz, NULL); } #endif /* WOLFSSL_HASH_KEEP */ diff --git a/wolfcrypt/src/hash.c b/wolfcrypt/src/hash.c index b733a3472..282c39f86 100644 --- a/wolfcrypt/src/hash.c +++ b/wolfcrypt/src/hash.c @@ -1707,7 +1707,7 @@ int wc_HashGetFlags(wc_HashAlg* hash, enum wc_HashType type, word32* flags) #endif /* !NO_HASH_WRAPPER */ #ifdef WOLFSSL_HASH_KEEP -int _wc_Sha_Grow(byte** msg, word32* used, word32* len, const byte* in, +int _wc_Hash_Grow(byte** msg, word32* used, word32* len, const byte* in, int inSz, void* heap) { if (*len < *used + inSz) { diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index eddd3820f..c893b05e4 100644 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -1713,13 +1713,13 @@ void wc_Sha256Free(wc_Sha256* sha256) */ int wc_Sha256_Grow(wc_Sha256* sha256, const byte* in, int inSz) { - return _wc_Sha_Grow(&(sha256->msg), &(sha256->used), &(sha256->len), in, + return _wc_Hash_Grow(&(sha256->msg), &(sha256->used), &(sha256->len), in, inSz, sha256->heap); } #ifdef WOLFSSL_SHA224 int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz) { - return _wc_Sha_Grow(&(sha224->msg), &(sha224->used), &(sha224->len), in, + return _wc_Hash_Grow(&(sha224->msg), &(sha224->used), &(sha224->len), in, inSz, sha224->heap); } #endif /* WOLFSSL_SHA224 */ diff --git a/wolfcrypt/src/sha512.c b/wolfcrypt/src/sha512.c index d9e4b3d32..f6c863665 100644 --- a/wolfcrypt/src/sha512.c +++ b/wolfcrypt/src/sha512.c @@ -1829,13 +1829,13 @@ int wc_Sha384GetFlags(wc_Sha384* sha384, word32* flags) */ int wc_Sha512_Grow(wc_Sha512* sha512, const byte* in, int inSz) { - return _wc_Sha_Grow(&(sha512->msg), &(sha512->used), &(sha512->len), in, + return _wc_Hash_Grow(&(sha512->msg), &(sha512->used), &(sha512->len), in, inSz, sha512->heap); } #ifdef WOLFSSL_SHA384 int wc_Sha384_Grow(wc_Sha384* sha384, const byte* in, int inSz) { - return _wc_Sha_Grow(&(sha384->msg), &(sha384->used), &(sha384->len), in, + return _wc_Hash_Grow(&(sha384->msg), &(sha384->used), &(sha384->len), in, inSz, sha384->heap); } #endif /* WOLFSSL_SHA384 */ diff --git a/wolfssl/wolfcrypt/hash.h b/wolfssl/wolfcrypt/hash.h index 6e5f32a84..81e176890 100644 --- a/wolfssl/wolfcrypt/hash.h +++ b/wolfssl/wolfcrypt/hash.h @@ -223,7 +223,7 @@ WOLFSSL_API int wc_Shake256Hash(const byte* data, word32 len, byte* hash, word32 #endif /* !NO_HASH_WRAPPER */ #if defined(WOLFSSL_HASH_KEEP) -WOLFSSL_LOCAL int _wc_Sha_Grow(byte** msg, word32* used, word32* len, +WOLFSSL_LOCAL int _wc_Hash_Grow(byte** msg, word32* used, word32* len, const byte* in, int inSz, void* heap); #endif