Skip to content

Commit 46fcac1

Browse files
tests: Add fuzzing harness for ec_seckey_import_der(...) and ec_seckey_export_der(...)
1 parent b667a90 commit 46fcac1

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
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_ec_seckey_import_export_der \
135136
test/fuzz/secp256k1_ecdsa_signature_parse_der_lax \
136137
test/fuzz/service_deserialize \
137138
test/fuzz/signature_checker \
@@ -1095,6 +1096,12 @@ test_fuzz_scriptnum_ops_LDADD = $(FUZZ_SUITE_LD_COMMON)
10951096
test_fuzz_scriptnum_ops_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
10961097
test_fuzz_scriptnum_ops_SOURCES = test/fuzz/scriptnum_ops.cpp
10971098

1099+
test_fuzz_secp256k1_ec_seckey_import_export_der_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
1100+
test_fuzz_secp256k1_ec_seckey_import_export_der_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
1101+
test_fuzz_secp256k1_ec_seckey_import_export_der_LDADD = $(FUZZ_SUITE_LD_COMMON)
1102+
test_fuzz_secp256k1_ec_seckey_import_export_der_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
1103+
test_fuzz_secp256k1_ec_seckey_import_export_der_SOURCES = test/fuzz/secp256k1_ec_seckey_import_export_der.cpp
1104+
10981105
test_fuzz_secp256k1_ecdsa_signature_parse_der_lax_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
10991106
test_fuzz_secp256k1_ecdsa_signature_parse_der_lax_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
11001107
test_fuzz_secp256k1_ecdsa_signature_parse_der_lax_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/key.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static secp256k1_context* secp256k1_context_sign = nullptr;
3131
*
3232
* out32 must point to an output buffer of length at least 32 bytes.
3333
*/
34-
static int ec_seckey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *seckey, size_t seckeylen) {
34+
int ec_seckey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *seckey, size_t seckeylen) {
3535
const unsigned char *end = seckey + seckeylen;
3636
memset(out32, 0, 32);
3737
/* sequence header */
@@ -88,7 +88,7 @@ static int ec_seckey_import_der(const secp256k1_context* ctx, unsigned char *out
8888
* will be set to the number of bytes used in the buffer.
8989
* key32 must point to a 32-byte raw private key.
9090
*/
91-
static int ec_seckey_export_der(const secp256k1_context *ctx, unsigned char *seckey, size_t *seckeylen, const unsigned char *key32, bool compressed) {
91+
int ec_seckey_export_der(const secp256k1_context *ctx, unsigned char *seckey, size_t *seckeylen, const unsigned char *key32, bool compressed) {
9292
assert(*seckeylen >= CKey::SIZE);
9393
secp256k1_pubkey pubkey;
9494
size_t pubkeylen = 0;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
int ec_seckey_import_der(const secp256k1_context* ctx, unsigned char* out32, const unsigned char* seckey, size_t seckeylen);
15+
int ec_seckey_export_der(const secp256k1_context* ctx, unsigned char* seckey, size_t* seckeylen, const unsigned char* key32, bool compressed);
16+
17+
void test_one_input(const std::vector<uint8_t>& buffer)
18+
{
19+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
20+
secp256k1_context* secp256k1_context_sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
21+
{
22+
std::vector<uint8_t> out32(32);
23+
(void)ec_seckey_import_der(secp256k1_context_sign, out32.data(), ConsumeFixedLengthByteVector(fuzzed_data_provider, CKey::SIZE).data(), CKey::SIZE);
24+
}
25+
{
26+
std::vector<uint8_t> seckey(CKey::SIZE);
27+
const std::vector<uint8_t> key32 = ConsumeFixedLengthByteVector(fuzzed_data_provider, 32);
28+
size_t seckeylen = CKey::SIZE;
29+
const bool compressed = fuzzed_data_provider.ConsumeBool();
30+
const bool exported = ec_seckey_export_der(secp256k1_context_sign, seckey.data(), &seckeylen, key32.data(), compressed);
31+
if (exported) {
32+
std::vector<uint8_t> out32(32);
33+
const bool imported = ec_seckey_import_der(secp256k1_context_sign, out32.data(), seckey.data(), seckey.size()) == 1;
34+
assert(imported && key32 == out32);
35+
}
36+
}
37+
secp256k1_context_destroy(secp256k1_context_sign);
38+
}

0 commit comments

Comments
 (0)