Skip to content
Open
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
16 changes: 8 additions & 8 deletions src/hash/clu_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Copy link
Copy Markdown
Contributor

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 convention

The PR correctly changes XSTRNCMP(alg, "sha", 3) to XSTRCMP(alg, "sha") to prevent the SHA-1 branch from matching sha256/sha384/sha512. However, other algorithm checks in both wolfCLU_hash() and wolfCLU_hashSetup() still use XSTRNCMP (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 accepts alg from external callers (e.g., clu_alg_hash.c). Switching all comparisons to XSTRCMP would be more defensive and consistent with the spirit of this fix — preventing future prefix-collision bugs if new algorithms are added.

Suggestion:

Suggested change
}
#ifndef NO_MD5
if (ret == WOLFCLU_SUCCESS && XSTRCMP(alg, "md5") == 0) {
ret = wc_Md5Hash(input, inputSz, output);
}
#endif
/* And similarly for sha256, sha384, sha512, blake2b, base64enc, base64dec
in both clu_hash.c and clu_hash_setup.c */

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed to use XSTRCMP

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 [Low] The fix is correct but the if-chain ordering is fragile
🔧 NIT style

In wolfCLU_hash(), all the hash algorithm branches are independent if statements (not else if). With the old XSTRNCMP(alg, "sha", 3) code, if alg was "sha256", both the sha256 branch (line 109) and the sha branch (line 124) would fire because ret was still WOLFCLU_SUCCESS (wc_Sha256Hash returns 0 on success). The SHA-1 branch would overwrite the SHA-256 output with a smaller (20-byte) digest. The fix to use XSTRCMP is correct. As a minor readability note, using else if for this chain would make it obvious that only one branch should ever execute, preventing similar issues in the future. This is not blocking since the fix as-is is correct.

Suggestion:

Suggested change
}
/* The #ifdef guards make else-if impossible across preprocessor blocks,
so XSTRCMP is the right fix here. No change needed. */

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 (#ifdef/#ifndef), you can’t safely use else if across those boundaries. Mutual exclusion is ensured by the ret == WOLFCLU_SUCCESS check and exact matching with XSTRCMP.

#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);
Expand All @@ -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 */
Expand Down
12 changes: 6 additions & 6 deletions src/hash/clu_hash_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int wolfCLU_hashSetup(int argc, char** argv)

for (i = 0; i < (int)algsSz; ++i) {
/* checks for acceptable algorithms */
if (XSTRNCMP(argv[2], algs[i], XSTRLEN(algs[i])) == 0) {
if (XSTRCMP(argv[2], algs[i]) == 0) {
alg = argv[2];
algCheck = 1;
}
Comment thread
dgarske marked this conversation as resolved.
Expand Down Expand Up @@ -135,27 +135,27 @@ int wolfCLU_hashSetup(int argc, char** argv)

/* sets default size of algorithm */
#ifndef NO_MD5
if (XSTRNCMP(alg, "md5", 3) == 0)
if (XSTRCMP(alg, "md5") == 0)
size = WC_MD5_DIGEST_SIZE;
#endif

#ifndef NO_SHA
if (XSTRNCMP(alg, "sha", 3) == 0)
if (XSTRCMP(alg, "sha") == 0)
size = WC_SHA_DIGEST_SIZE;
#endif

#ifndef NO_SHA256
if (XSTRNCMP(alg, "sha256", 6) == 0)
if (XSTRCMP(alg, "sha256") == 0)
size = WC_SHA256_DIGEST_SIZE;
#endif

#ifdef WOLFSSL_SHA384
if (XSTRNCMP(alg, "sha384", 6) == 0)
if (XSTRCMP(alg, "sha384") == 0)
size = WC_SHA384_DIGEST_SIZE;
#endif

#ifdef WOLFSSL_SHA512
if (XSTRNCMP(alg, "sha512", 6) == 0)
if (XSTRCMP(alg, "sha512") == 0)
size = WC_SHA512_DIGEST_SIZE;
#endif

Expand Down
Loading