Skip to content

Commit 974af91

Browse files
Chandra Prataprustyrussell
authored andcommitted
fuzz-tests: add a check for bolt11_encode()
Changelog-None: Since `bolt11_decode()` defined in `common/bolt11.c` is untested by the current BOLT #11 fuzz test, add a test for it.
1 parent fc549b3 commit 974af91

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

tests/fuzz/fuzz-bolt11.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "config.h"
22

33
#include <bitcoin/chainparams.h>
4+
#include <bitcoin/privkey.h>
45
#include <common/bech32.h>
56
#include <common/bolt11.h>
67
#include <common/features.h>
@@ -42,6 +43,35 @@ static size_t initial_input(uint8_t *fuzz_data, size_t size, size_t max_size)
4243
return size;
4344
}
4445

46+
static bool test_sign(const u5 *u5bytes,
47+
const u8 *hrpu8,
48+
secp256k1_ecdsa_recoverable_signature *rsig,
49+
void *unused UNUSED)
50+
{
51+
struct hash_u5 hu5;
52+
char *hrp;
53+
struct sha256 sha;
54+
struct privkey privkey;
55+
56+
memset(&privkey, 'a', sizeof(privkey));
57+
58+
hrp = tal_dup_arr(NULL, char, (char *)hrpu8, tal_count(hrpu8), 1);
59+
hrp[tal_count(hrpu8)] = '\0';
60+
61+
hash_u5_init(&hu5, hrp);
62+
hash_u5(&hu5, u5bytes, tal_count(u5bytes));
63+
hash_u5_done(&hu5, &sha);
64+
tal_free(hrp);
65+
66+
if (!secp256k1_ecdsa_sign_recoverable(secp256k1_ctx, rsig,
67+
(const u8 *)&sha,
68+
privkey.secret.data,
69+
NULL, NULL))
70+
abort();
71+
72+
return true;
73+
}
74+
4575
// We use a custom mutator to produce an input corpus that consists entirely of
4676
// correctly encoded bech32 strings. This enables us to efficiently fuzz the
4777
// bolt11 decoding logic without the fuzzer getting stuck on fuzzing the bech32
@@ -187,9 +217,13 @@ size_t LLVMFuzzerCustomCrossOver(const u8 *in1, size_t in1_size, const u8 *in2,
187217
void run(const uint8_t *data, size_t size)
188218
{
189219
char *invoice_str = to_string(tmpctx, data, size);
190-
char *fail;
220+
char *fail = NULL;
191221

192-
bolt11_decode(tmpctx, invoice_str, NULL, NULL, NULL, &fail);
222+
struct bolt11 *b11 = bolt11_decode(tmpctx, invoice_str, NULL, NULL, NULL, &fail);
223+
if (b11)
224+
bolt11_encode(tmpctx, b11, false, test_sign, NULL);
225+
else
226+
assert(fail);
193227

194228
clean_tmpctx();
195229
}

0 commit comments

Comments
 (0)