Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

gojimmypi
Copy link
Contributor

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 reasonable buffLen is passed for wc_ShaFinal(), Sha256Final(), and Sha512Final().

If a valid buffLen value is not found, the function will return with BAD_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

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

@gojimmypi gojimmypi self-assigned this Jan 22, 2023
@gojimmypi gojimmypi marked this pull request as draft January 22, 2023 19:06
@gojimmypi gojimmypi marked this pull request as ready for review January 22, 2023 21:09
@dgarske dgarske self-assigned this Jan 23, 2023
@@ -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 */

@@ -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.

Just remove the #ifndef WC_NO_HARDEN

@dgarske dgarske removed their assignment Jan 24, 2023
Copy link
Contributor

@SparkiDev SparkiDev left a 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;
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.

@gojimmypi gojimmypi marked this pull request as draft February 8, 2023 22:58
@wolfSSL-Bot
Copy link

Can one of the admins verify this patch?

@gojimmypi
Copy link
Contributor Author

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

@gojimmypi gojimmypi closed this Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants