-
Notifications
You must be signed in to change notification settings - Fork 39
F 1483 : Fix SHA-1 prefix match overwriting SHA-256/384/512 output #216
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -101,32 +101,32 @@ int wolfCLU_hash(WOLFSSL_BIO* bioIn, WOLFSSL_BIO* bioOut, const char* alg, | |||||||
|
|
||||||||
| /* hashes using accepted algorithm */ | ||||||||
| #ifndef NO_MD5 | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRNCMP(alg, "md5", 3) == 0) { | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRCMP(alg, "md5") == 0) { | ||||||||
| ret = wc_Md5Hash(input, inputSz, output); | ||||||||
| } | ||||||||
| #endif | ||||||||
| #ifndef NO_SHA256 | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRNCMP(alg, "sha256", 6) == 0) { | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRCMP(alg, "sha256") == 0) { | ||||||||
| ret = wc_Sha256Hash(input, inputSz, output); | ||||||||
| } | ||||||||
| #endif | ||||||||
| #ifdef WOLFSSL_SHA384 | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRNCMP(alg, "sha384", 6) == 0) { | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRCMP(alg, "sha384") == 0) { | ||||||||
| ret = wc_Sha384Hash(input, inputSz, output); | ||||||||
| } | ||||||||
| #endif | ||||||||
| #ifdef WOLFSSL_SHA512 | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRNCMP(alg, "sha512", 6) == 0) { | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRCMP(alg, "sha512") == 0) { | ||||||||
| ret = wc_Sha512Hash(input, inputSz, output); | ||||||||
| } | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 [Low] The fix is correct but the if-chain ordering is fragile In Suggestion:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed on readability. However, since each branch is wrapped in a preprocessor guard ( |
||||||||
| #endif | ||||||||
| #ifndef NO_SHA | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRNCMP(alg, "sha", 3) == 0) { | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRCMP(alg, "sha") == 0) { | ||||||||
| ret = wc_ShaHash(input, inputSz, output); | ||||||||
| } | ||||||||
| #endif | ||||||||
| #ifdef HAVE_BLAKE2 | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRNCMP(alg, "blake2b", 7) == 0) { | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRCMP(alg, "blake2b") == 0) { | ||||||||
| ret = wc_InitBlake2b(&hash, size); | ||||||||
| if (ret != 0) return ret; | ||||||||
| ret = wc_Blake2bUpdate(&hash, input, inputSz); | ||||||||
|
|
@@ -138,11 +138,11 @@ int wolfCLU_hash(WOLFSSL_BIO* bioIn, WOLFSSL_BIO* bioOut, const char* alg, | |||||||
|
|
||||||||
| #ifndef NO_CODING | ||||||||
| #ifdef WOLFSSL_BASE64_ENCODE | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRNCMP(alg, "base64enc", 9) == 0) { | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRCMP(alg, "base64enc") == 0) { | ||||||||
| ret = Base64_Encode(input, inputSz, output, (word32*)&size); | ||||||||
| } | ||||||||
| #endif /* WOLFSSL_BASE64_ENCODE */ | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRNCMP(alg, "base64dec", 9) == 0) { | ||||||||
| if (ret == WOLFCLU_SUCCESS && XSTRCMP(alg, "base64dec") == 0) { | ||||||||
| ret = Base64_Decode(input, inputSz, output, (word32*)&size); | ||||||||
| } | ||||||||
| #endif /* !NO_CODING */ | ||||||||
|
|
||||||||
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.
🟡 [Medium] Remaining XSTRNCMP calls could be made consistent with the fix
💡 SUGGEST
conventionThe PR correctly changes
XSTRNCMP(alg, "sha", 3)toXSTRCMP(alg, "sha")to prevent the SHA-1 branch from matching sha256/sha384/sha512. However, other algorithm checks in bothwolfCLU_hash()andwolfCLU_hashSetup()still useXSTRNCMP(e.g.,XSTRNCMP(alg, "md5", 3)at clu_hash.c:104,XSTRNCMP(alg, "md5", 3)at clu_hash_setup.c:138). While these are functionally safe today because none of those prefixes collide with other algorithm names,wolfCLU_hash()is a public function that acceptsalgfrom external callers (e.g.,clu_alg_hash.c). Switching all comparisons toXSTRCMPwould be more defensive and consistent with the spirit of this fix — preventing future prefix-collision bugs if new algorithms are added.Suggestion:
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.
Changed to use
XSTRCMP