-
Notifications
You must be signed in to change notification settings - Fork 836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check final SHA buffer blocksize #5998
Conversation
@@ -719,6 +719,14 @@ int wc_ShaFinal(wc_Sha* sha, byte* hash) | |||
return BAD_FUNC_ARG; | |||
} | |||
|
|||
#ifndef WC_NO_HARDEN |
There was a problem hiding this comment.
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 */
@@ -719,6 +719,14 @@ int wc_ShaFinal(wc_Sha* sha, byte* hash) | |||
return BAD_FUNC_ARG; | |||
} | |||
|
|||
#ifndef WC_NO_HARDEN |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise fine.
/* 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; |
There was a problem hiding this comment.
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.
Can one of the admins verify this patch? |
I'm closing this lingering PR. I'd still like to do this at some point. I've added this to my Mothball section in #6234 |
Description
During the final block processing of the SHA2 hash, there's an
0x80
byte value added to the buffer and the remaining buffer bytes are set to zero. This PR ensures a reasonablebuffLen
is passed forwc_ShaFinal()
,Sha256Final()
, andSha512Final()
.If a valid
buffLen
value is not found, the function will return withBAD_FUNC_ARG
.It should be relatively unlikely that the wrong size would be encountered. This check should have relatively small impact. However, if every possible bit of performance is desired, this feature can be disabled with
#define WC_NO_HARDEN
.The length was problematic while testing a variety of scenarios related to #5948 and #5997, thus this PR was created for a more graceful handling of a bad buffer length.
Fixes zd# n/a
Testing
Confirmed wolfcrypt/test completed with success. (In my case on the ESP32, but expected to pass on other platforms as well)
Checklist