Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions wolfcrypt/src/sha.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,14 @@ int wc_ShaFinal(wc_Sha* sha, byte* hash)
return BAD_FUNC_ARG;
}

#ifndef WC_NO_HARDEN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move all this down to where the 0x80 is set. Use BUFFER_E as the failure. Inline with the error check on the update. Please fix other algos to use this too.

% git diff
diff --git a/wolfcrypt/src/sha.c b/wolfcrypt/src/sha.c
index bcfd1005d..3037d7cab 100644
--- a/wolfcrypt/src/sha.c
+++ b/wolfcrypt/src/sha.c
@@ -719,8 +719,6 @@ int wc_ShaFinal(wc_Sha* sha, byte* hash)
         return BAD_FUNC_ARG;
     }

-    local = (byte*)sha->buffer;
-
 #ifdef WOLF_CRYPTO_CB
     if (sha->devId != INVALID_DEVID) {
         ret = wc_CryptoCb_ShaHash(sha, NULL, 0, hash);
@@ -737,6 +735,11 @@ int wc_ShaFinal(wc_Sha* sha, byte* hash)
     }
 #endif /* WOLFSSL_ASYNC_CRYPT */

+    if (sha->buffLen > WC_SHA_BLOCK_SIZE - 1) {
+        return BUFFER_E;
+    }
+
+    local = (byte*)sha->buffer;
     local[sha->buffLen++] = 0x80;  /* add 1 */

     /* pad with zeros */

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remove the #ifndef WC_NO_HARDEN

/* We'll add a 0x80 byte at the end,
** so make sure we have appropriate buffer length. */
if (sha->buffLen > WC_SHA_BLOCK_SIZE - 1) {
return BAD_FUNC_ARG;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an internal error.
It shouldn't happen when the implementation is working appropriately.
The best error code I've found for this kind of issue is BAD_STATE_E.

}
#endif

local = (byte*)sha->buffer;

#ifdef WOLF_CRYPTO_CB
Expand Down
8 changes: 8 additions & 0 deletions wolfcrypt/src/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,14 @@ static int InitSha256(wc_Sha256* sha256)
return BAD_FUNC_ARG;
}

#ifndef WC_NO_HARDEN
/* We'll add a 0x80 byte at the end,
** so make sure we have appropriate buffer length. */
if (sha256->buffLen > WC_SHA256_BLOCK_SIZE - 1) {
return BAD_FUNC_ARG;
}
#endif

local = (byte*)sha256->buffer;
local[sha256->buffLen++] = 0x80; /* add 1 */

Expand Down
8 changes: 8 additions & 0 deletions wolfcrypt/src/sha512.c
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,14 @@ static WC_INLINE int Sha512Final(wc_Sha512* sha512)
return BAD_FUNC_ARG;
}

#ifndef WC_NO_HARDEN
/* We'll add a 0x80 byte at the end,
** so make sure we have appropriate buffer length. */
if (sha512->buffLen > WC_SHA512_BLOCK_SIZE - 1) {
return BAD_FUNC_ARG;
}
#endif

local = (byte*)sha512->buffer;

local[sha512->buffLen++] = 0x80; /* add 1 */
Expand Down