SHA-3/SHAKE/EVP_ zeroed ctx guard - #3454
Open
manastasova wants to merge 3 commits into
Open
Conversation
Contributor
|
🔒 Security Review — View Report Please review before merging. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3454 +/- ##
==========================================
+ Coverage 78.06% 78.27% +0.21%
==========================================
Files 700 700
Lines 124705 124726 +21
Branches 17326 17332 +6
==========================================
+ Hits 97352 97634 +282
+ Misses 26484 26224 -260
+ Partials 869 868 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context and motivation
KECCAK1600_STATE_ABSORBflag was set to0, so an all-zeroKECCAK1600_CTX— never initialised, or cleansed after finalisation — was indistinguishable from one legitimately mid-absorb. The phase checks were deny-lists (state == SQUEEZE || state == FINAL), so such a context reached the primitives withblock_size == 0:Keccak1600_Absorbspins forever onwhile (len >= r)withr == 0, andKeccakSponge_AbsorbFinalindexesctx->buf[block_size - 1].The same context is reachable through EVP:
EVP_DigestFinal_excleansesmd_databut leavesctx->digestset, so a second call re-entersfinal. That slot returnsvoid, soSHA3_Finalrefusing the call could not be reported and surfaced as an abort.Description of changes
Adds
KECCAK1600_STATE_UNINIT = 0and renumbers the rest (ABSORB = 1,SQUEEZE = 2,FINAL = 3), giving a zeroed context a distinct, invalid phase. The checks become allow-lists (state != KECCAK1600_STATE_ABSORB), rejecting uninitialised and already-finalised contexts alike.Keccak1600_Squeeze'spaddedparameter becomesstate.At the EVP layer, adds
EVP_MD_CTX_FINALISED, set byEVP_DigestFinal_exand cleared byEVP_DigestInit_ex, so a second finalisation returns 0 instead of re-entering the digest. Also fixesused_for_hmacto maskEVP_MD_CTX_HMACrather than compare the whole flags word.Testing
New
sha3_test.cccases drive a zeroed context throughSHA3_Update/SHA3_FinalandSHAKE_Absorb/SHAKE_Final/SHAKE_Squeeze, confirm the existing phase transitions are unchanged, and pinKeccak1600_Squeeze's phase semantics againstSHAKE128.keccak256_test.ccadds streaming-misuse and EVP double-final coverage. Without the guards these tests hang or abort rather than fail.Review considerations
Public behaviour change: a second
EVP_DigestFinal_exwithout re-initialising now returns 0 for every digest, where some previously aborted and others silently returned success. This diverges from OpenSSL/BoringSSL;include/openssl/digest.hdocuments it.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.