Skip to content

fuzz-tests: Add differential test for HMAC-SHA256 #8185

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Chand-ra
Copy link

Add a differential fuzz test for HMAC-SHA256, similar to those
for SHA256 and RIPEMD160, to verify CCAN’s implementation
against OpenSSL’s.

Makefile Outdated
@@ -283,7 +283,7 @@ ifeq ($(STATIC),1)
# But that doesn't static link.
LDLIBS = -L$(CPATH) -Wl,-dn $(SQLITE3_LDLIBS) -Wl,-dy -lm -lpthread -ldl $(COVFLAGS)
else
LDLIBS = -L$(CPATH) -lm $(SQLITE3_LDLIBS) $(COVFLAGS)
Copy link
Member

Choose a reason for hiding this comment

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

Question for the linker experts: does this end up linking CLN against openssl in release builds? If yes, we should reduce the linker arg changes to just impact the fuzz testing.

Copy link
Author

Choose a reason for hiding this comment

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

Question for the linker experts: does this end up linking CLN against openssl in release builds? If yes, we should reduce the linker arg changes to just impact the fuzz testing.

You're right, that's an error on my part. I should've instead added this line in tests/fuzz/Makefile:
tests/fuzz/fuzz-hmac-sha256: LDLIBS += -lcrypto

which is similar to the linking routine for SHA256 and RIPEMD160 fuzz tests. I'll add this change in the next push.

@Chand-ra
Copy link
Author

Chand-ra commented Apr 16, 2025

CC: @morehouse

@cdecker
Copy link
Member

cdecker commented Apr 21, 2025

Hm, these fuzz corpora are getting rather large. Is there any way of making them use fewer files, or we can make them optional? They take rather long to materialize on disk, slowing down git considerably, and we run fuzzing mostly on dedicated machines anyway.

@morehouse
Copy link
Contributor

@cdecker There's multiple benefits to having the corpora stored publicly.

  1. Everyone benefits from new inputs being shared. New contributors can start fuzzing from the latest corpus instead of starting from scratch. Likewise long-time CLN contributors can use new inputs found by newer contributors. Running fuzz tests is an easy way for newcomers to contribute to CLN, and sharing corpora helps encourage this kind of outside contribution.
  2. The public corpora act as regression tests in CLN's CI, so breaking changes are found before PRs are merged.

Of course, the corpora don't need to live inside the CLN repo itself. For example, Bitcoin Core and LND both store their corpora in separate repos. The main benefits of having them co-located with CLN is ease-of-use and better visibility.

Copy link
Contributor

@morehouse morehouse left a comment

Choose a reason for hiding this comment

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

Concept ACK. Will review the target this week.

@cdecker
Copy link
Member

cdecker commented Apr 22, 2025

Oh so t get me wrong, the corpora should be public, and running should be as simple as possible. My main issue is that were creating and tracking thousands of files, that will not change. I was mostly thinking if there is a way to bundle the corpus into a tarball and track that instead.

@morehouse
Copy link
Contributor

The corpora do change though, as new inputs are found. How do you propose we compare and review tarballs when that happens?

If you don't want the corpora stored in the CLN repo, I'd recommend creating a separate repo to store them uncompressed.

Copy link
Contributor

@morehouse morehouse left a comment

Choose a reason for hiding this comment

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

Nice target, looks mostly good.

Comment on lines 14 to 15
static const unsigned char hmac_key[] = "fuzz";
static const size_t hmac_key_len = sizeof(hmac_key) - 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be worth allowing the fuzzer to choose the key.

size_t hash_size;
EVP_MAC_CTX *ctx;
OSSL_PARAM params[] = {
OSSL_PARAM_construct_utf8_string("digest", "SHA256", 0),
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this actually work? I thought the MD algorithm was named SHA-256.

Copy link
Author

Choose a reason for hiding this comment

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

Apparently SHA-256, SHA2-256 and SHA256 are all aliases to fetch the SHA-256 digest.

Chandra Pratap added 2 commits April 23, 2025 16:47
Changelog-None: Add a differential fuzz test for
HMAC-SHA256, similar to those for SHA256 and RIPEMD160,
to verify CCAN’s implementation against OpenSSL’s.
Add a minimal input set as a seed corpus for the newly introduced
test. This leads to discovery of interesting code paths faster.
Comment on lines +71 to +72
hmac_key = (unsigned char*) data;
hmac_key_len = size;
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't the right way to let the fuzzer choose the key, as the key is no longer independent of the actual data being HMAC'd.

Instead we should peel off some prefix of data as the key before doing the rest of the test, just like we do for num_splits. In this case, I'd suggest using the first byte of data as the size of the key, then peel off that many subsequent bytes as the key itself. Finally peel off num_splits and leave the rest of the data to be HMAC'd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants