Skip to content

Commit b667a90

Browse files
tests: Add fuzzing harness for SigHasLowR(...) and ecdsa_signature_parse_der_lax(...)
1 parent a2a250c commit b667a90

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ FUZZ_TARGETS = \
132132
test/fuzz/script_sigcache \
133133
test/fuzz/script_sign \
134134
test/fuzz/scriptnum_ops \
135+
test/fuzz/secp256k1_ecdsa_signature_parse_der_lax \
135136
test/fuzz/service_deserialize \
136137
test/fuzz/signature_checker \
137138
test/fuzz/snapshotmetadata_deserialize \
@@ -1094,6 +1095,12 @@ test_fuzz_scriptnum_ops_LDADD = $(FUZZ_SUITE_LD_COMMON)
10941095
test_fuzz_scriptnum_ops_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
10951096
test_fuzz_scriptnum_ops_SOURCES = test/fuzz/scriptnum_ops.cpp
10961097

1098+
test_fuzz_secp256k1_ecdsa_signature_parse_der_lax_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
1099+
test_fuzz_secp256k1_ecdsa_signature_parse_der_lax_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
1100+
test_fuzz_secp256k1_ecdsa_signature_parse_der_lax_LDADD = $(FUZZ_SUITE_LD_COMMON)
1101+
test_fuzz_secp256k1_ecdsa_signature_parse_der_lax_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
1102+
test_fuzz_secp256k1_ecdsa_signature_parse_der_lax_SOURCES = test/fuzz/secp256k1_ecdsa_signature_parse_der_lax.cpp
1103+
10971104
test_fuzz_service_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DSERVICE_DESERIALIZE=1
10981105
test_fuzz_service_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
10991106
test_fuzz_service_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/pubkey.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ secp256k1_context* secp256k1_context_verify = nullptr;
2424
* strict DER before being passed to this module, and we know it supports all
2525
* violations present in the blockchain before that point.
2626
*/
27-
static int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
27+
int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
2828
size_t rpos, rlen, spos, slen;
2929
size_t pos = 0;
3030
size_t lenbyte;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <key.h>
6+
#include <secp256k1.h>
7+
#include <test/fuzz/FuzzedDataProvider.h>
8+
#include <test/fuzz/fuzz.h>
9+
#include <test/fuzz/util.h>
10+
11+
#include <cstdint>
12+
#include <vector>
13+
14+
bool SigHasLowR(const secp256k1_ecdsa_signature* sig);
15+
int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char* input, size_t inputlen);
16+
17+
void test_one_input(const std::vector<uint8_t>& buffer)
18+
{
19+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
20+
const std::vector<uint8_t> signature_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
21+
if (signature_bytes.data() == nullptr) {
22+
return;
23+
}
24+
secp256k1_context* secp256k1_context_verify = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
25+
secp256k1_ecdsa_signature sig_der_lax;
26+
const bool parsed_der_lax = ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig_der_lax, signature_bytes.data(), signature_bytes.size()) == 1;
27+
if (parsed_der_lax) {
28+
ECC_Start();
29+
(void)SigHasLowR(&sig_der_lax);
30+
ECC_Stop();
31+
}
32+
secp256k1_context_destroy(secp256k1_context_verify);
33+
}

0 commit comments

Comments
 (0)