global: replace randombytes_buf() with randbytes() wrapper.
What changed, and why it matters
This change is a straightforward internal cleanup: it replaces direct calls to the cryptographic library's random-byte function (randombytes_buf from libsodium) with a project-specific wrapper called randbytes(). The wrapper currently does the same thing, but having it in one place makes it easier to swap in a deterministic (predictable) random source for testing in the future. There is no direct security bug introduced by this commit itself, and no evidence it weakens real-world randomness.
No urgent action. Treat as normal refactoring. If reviewing follow-up work, verify that any deterministic randbytes_() override is used only in test/fuzz builds and never compiled into production binaries, and that the wrapper continues to use a CSPRNG (e.g., libsodium's randombytes_buf) for live code.
Security signals we found
Refactoring of cryptographic randomness API
New wrapper enables future deterministic override in tests
No change to production entropy source in this commit
Makefile lint rule now discourages direct randombytes_buf() usage
Evidence from the diff
The commit mechanically substitutes randombytes_buf() with a new project wrapper randbytes() across 69 files, including production code (connectd, hsmd, invoice, plugins, sphinx, onion_message) and many unit tests. The wrapper is defined in common/randbytes.c/h and, in production, still delegates to randombytes_buf(). The primary motivation stated in the commit message is test determinism: tests can override randbytes_() to supply predictable randomness. The Makefile’s discouraged-function check is updated to flag future direct uses of randombytes_buf(). Some test mocks are adjusted to intercept randbytes() instead of randombytes_buf(). No algorithmic change to entropy sourcing occurs in production builds.
Changed components
common/randbytes (new wrapper)connectd/connectd.cconnectd/handshake.chsmd/hsmd.clightningd/invoice.ccommon/sphinx.ccommon/onion_message.cplugins/fetchinvoice.cplugins/keysend.cplugins/offers_invreq_hook.cplugins/offers_offer.ccommon/pseudorand.cbitcoin/short_channel_id.cbitcoin/script.ctests/fuzz/connectd_handshake.hunit test harnesses in common/test, connectd/test, onchaind/test, plugins/renepay/test, wire/testInspect captured patch +112 / −45
diff --git a/Makefile b/Makefile
index 54a3bbe..07f1a19 100644
--- a/Makefile
+++ b/Makefile
@@ -603,7 +603,7 @@ check-tmpctx:
@if git grep -n 'tal_free[(]tmpctx)' | grep -Ev '^ccan/|/test/|^common/setup.c:|^common/utils.c:'; then echo "Don't free tmpctx!">&2; exit 1; fi
check-discouraged-functions:
- @if git grep -E "[^a-z_/](fgets|fputs|gets|scanf|sprintf)\(" -- "*.c" "*.h" ":(exclude)ccan/" ":(exclude)contrib/"; then exit 1; fi
+ @if git grep -nE "[^a-z_/](fgets|fputs|gets|scanf|sprintf|randombytes_buf)\(" -- "*.c" "*.h" ":(exclude)ccan/" ":(exclude)contrib/" | grep -Fv '/* discouraged:'; then exit 1; fi
check-bad-sprintf:
@if git grep -n "%[*]\.s"; then exit 1; fi
diff --git a/bitcoin/script.c b/bitcoin/script.c
index 1c703db..2422f61 100644
--- a/bitcoin/script.c
+++ b/bitcoin/script.c
@@ -6,8 +6,8 @@
#include <bitcoin/pubkey.h>
#include <bitcoin/script.h>
#include <ccan/mem/mem.h>
+#include <common/randbytes.h>
#include <common/utils.h>
-#include <sodium/randombytes.h>
#include <wally_script.h>
/* To push 0-75 bytes onto stack. */
diff --git a/bitcoin/short_channel_id.c b/bitcoin/short_channel_id.c
index cc1de68..98fe474 100644
--- a/bitcoin/short_channel_id.c
+++ b/bitcoin/short_channel_id.c
@@ -1,7 +1,7 @@
#include "config.h"
#include <bitcoin/short_channel_id.h>
#include <ccan/tal/str/str.h>
-#include <sodium/randombytes.h>
+#include <common/randbytes.h>
#include <stdio.h>
#include <wire/wire.h>
@@ -104,6 +104,6 @@ struct short_channel_id fromwire_short_channel_id(const u8 **cursor, size_t *max
struct short_channel_id random_scid(void)
{
struct short_channel_id scid;
- randombytes_buf(&scid, sizeof(scid));
+ randbytes(&scid, sizeof(scid));
return scid;
}
diff --git a/channeld/test/Makefile b/channeld/test/Makefile
index 0bb50c4..f683ec3 100644
--- a/channeld/test/Makefile
+++ b/channeld/test/Makefile
@@ -27,6 +27,7 @@ channeld/test/run-full_channel: \
common/msg_queue.o \
common/permute_tx.o \
common/pseudorand.o \
+ common/randbytes.o \
common/setup.o \
common/utils.o
diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c
index a852d19..1bb406a 100644
--- a/channeld/test/run-full_channel.c
+++ b/channeld/test/run-full_channel.c
@@ -11,6 +11,7 @@
#include <ccan/err/err.h>
#include <ccan/io/io.h>
#include <common/daemon.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <wire/wire_io.h>
#include <wire/wire_sync.h>
diff --git a/common/blindedpath.h b/common/blindedpath.h
index 7a5238f..96a55ed 100644
--- a/common/blindedpath.h
+++ b/common/blindedpath.h
@@ -22,7 +22,7 @@ struct tlv_encrypted_data_tlv_payment_relay;
* @next_path_privkey: (out) e(i+1), the next blinding secret (optional)
* @node_alias: (out) the blinded pubkey of the node to tell the recipient.
*
- * You create a blinding secret using randombytes_buf(), then call this
+ * You create a blinding secret using randbytes(), then call this
* iteratively for each node in the path.
*/
u8 *encrypt_tlv_encrypted_data(const tal_t *ctx,
diff --git a/common/onion_message.c b/common/onion_message.c
index 34faf00..25e83df 100644
--- a/common/onion_message.c
+++ b/common/onion_message.c
@@ -4,6 +4,7 @@
#include <ccan/cast/cast.h>
#include <common/blindedpath.h>
#include <common/onion_message.h>
+#include <common/randbytes.h>
#include <common/sphinx.h>
#include <sodium.h>
@@ -66,7 +67,7 @@ struct blinded_path *blinded_path_from_encdata_tlvs(const tal_t *ctx,
assert(nhops > 0);
assert(tal_count(ids) > 0);
- randombytes_buf(&first_blinding, sizeof(first_blinding));
+ randbytes(&first_blinding, sizeof(first_blinding));
if (!pubkey_from_privkey(&first_blinding, &path->first_path_key))
abort();
sciddir_or_pubkey_from_pubkey(&path->first_node_id, &ids[0]);
diff --git a/common/pseudorand.c b/common/pseudorand.c
index b29aeee..7f3efaf 100644
--- a/common/pseudorand.c
+++ b/common/pseudorand.c
@@ -4,7 +4,7 @@
#include <ccan/isaac/isaac64.h>
#include <ccan/tal/tal.h>
#include <common/pseudorand.h>
-#include <sodium/randombytes.h>
+#include <common/randbytes.h>
static struct isaac64_ctx isaac64;
static struct siphash_seed siphashseed;
@@ -16,7 +16,7 @@ static void init_if_needed(void)
unsigned char seedbuf[16];
struct sha256 sha;
- randombytes_buf(seedbuf, sizeof(seedbuf));
+ randbytes(seedbuf, sizeof(seedbuf));
memcpy(&siphashseed, seedbuf, sizeof(siphashseed));
/* In case isaac is reversible, don't leak seed. */
diff --git a/common/sphinx.c b/common/sphinx.c
index dd405a0..6ac128d 100644
--- a/common/sphinx.c
+++ b/common/sphinx.c
@@ -5,13 +5,13 @@
#include <ccan/mem/mem.h>
#include <common/onionreply.h>
#include <common/overflows.h>
+#include <common/randbytes.h>
#include <common/sphinx.h>
#include <common/utils.h>
#include <secp256k1_ecdh.h>
#include <sodium/crypto_stream_chacha20.h>
-#include <sodium/randombytes.h>
#define BLINDING_FACTOR_SIZE 32
@@ -566,7 +566,7 @@ struct onionpacket *create_onionpacket(
if (sp->session_key == NULL) {
sp->session_key = tal(sp, struct secret);
- randombytes_buf(sp->session_key, sizeof(struct secret));
+ randbytes(sp->session_key, sizeof(struct secret));
}
params = generate_hop_params(ctx, sp->session_key->data, sp);
diff --git a/common/test/Makefile b/common/test/Makefile
index 8ae6a41..e13bfb3 100644
--- a/common/test/Makefile
+++ b/common/test/Makefile
@@ -5,6 +5,7 @@ COMMON_TEST_PROGRAMS := $(COMMON_TEST_OBJS:.o=)
COMMON_TEST_COMMON_OBJS := \
common/autodata.o \
+ common/randbytes.o \
common/setup.o \
common/utils.o
@@ -119,6 +120,7 @@ common/test/run-trace: \
common/amount.o \
common/memleak.o \
common/pseudorand.o \
+ common/randbytes.o \
common/trace.o \
wire/fromwire.o \
wire/towire.o
diff --git a/common/test/run-amount.c b/common/test/run-amount.c
index 0e9f295..f76484e 100644
--- a/common/test/run-amount.c
+++ b/common/test/run-amount.c
@@ -1,5 +1,6 @@
#include "config.h"
#include "../amount.c"
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
diff --git a/common/test/run-base64.c b/common/test/run-base64.c
index c36c08b..e71371c 100644
--- a/common/test/run-base64.c
+++ b/common/test/run-base64.c
@@ -3,6 +3,7 @@
#include <assert.h>
#include <ccan/base64/base64.h>
#include <common/amount.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <common/utils.h>
#include <sodium/utils.h>
diff --git a/common/test/run-bigsize.c b/common/test/run-bigsize.c
index 8ff40e5..1e167fd 100644
--- a/common/test/run-bigsize.c
+++ b/common/test/run-bigsize.c
@@ -4,6 +4,7 @@
#include <common/amount.h>
#include <common/json_parse.c>
#include <common/json_parse_simple.c>
+#include <common/randbytes.h>
#include <common/setup.h>
static const char *reason;
diff --git a/common/test/run-blindedpath_enctlv.c b/common/test/run-blindedpath_enctlv.c
index c9408cb..d2552ad 100644
--- a/common/test/run-blindedpath_enctlv.c
+++ b/common/test/run-blindedpath_enctlv.c
@@ -3,6 +3,7 @@
#include "../blinding.c"
#include "../hmac.c"
#include <common/channel_id.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
diff --git a/common/test/run-bolt11.c b/common/test/run-bolt11.c
index e4fbbd7..bd8c2c6 100644
--- a/common/test/run-bolt11.c
+++ b/common/test/run-bolt11.c
@@ -6,11 +6,12 @@
#include "../features.c"
#include "../node_id.c"
#include "../hash_u5.c"
-#include "../memleak.c"
#include "../wire/fromwire.c"
#include "../wire/towire.c"
#include <ccan/err/err.h>
+#include <common/randbytes.h>
#include <common/setup.h>
+#include <stdio.h>
/* AUTOGENERATED MOCKS START */
/* AUTOGENERATED MOCKS END */
diff --git a/common/test/run-bolt12-encode-test.c b/common/test/run-bolt12-encode-test.c
index e7de25f..5ca641f 100644
--- a/common/test/run-bolt12-encode-test.c
+++ b/common/test/run-bolt12-encode-test.c
@@ -12,6 +12,7 @@
#include <common/bolt12.c>
#include <common/bolt12_merkle.h>
#include <common/features.c>
+#include <common/randbytes.h>
#include <common/sciddir_or_pubkey.c>
#include <common/setup.h>
#include <inttypes.h>
diff --git a/common/test/run-bolt12-format-string-test.c b/common/test/run-bolt12-format-string-test.c
index e120af6..f2a5e11 100644
--- a/common/test/run-bolt12-format-string-test.c
+++ b/common/test/run-bolt12-format-string-test.c
@@ -8,6 +8,7 @@
#include <common/bolt12.h>
#include <common/bolt12_merkle.h>
#include <common/features.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <common/utils.h>
diff --git a/common/test/run-bolt12-offer-decode.c b/common/test/run-bolt12-offer-decode.c
index 6febbb3..329a139 100644
--- a/common/test/run-bolt12-offer-decode.c
+++ b/common/test/run-bolt12-offer-decode.c
@@ -14,6 +14,7 @@
#include <ccan/array_size/array_size.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>
+#include <common/randbytes.h>
#include <common/setup.h>
/* AUTOGENERATED MOCKS START */
diff --git a/common/test/run-bolt12_decode.c b/common/test/run-bolt12_decode.c
index 9e5333b..e4473ad 100644
--- a/common/test/run-bolt12_decode.c
+++ b/common/test/run-bolt12_decode.c
@@ -8,6 +8,7 @@
#include <ccan/json_escape/json_escape.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>
+#include <common/randbytes.h>
#include <common/setup.h>
/* AUTOGENERATED MOCKS START */
diff --git a/common/test/run-bolt12_merkle-json.c b/common/test/run-bolt12_merkle-json.c
index 3c7d5a0..fefac76 100644
--- a/common/test/run-bolt12_merkle-json.c
+++ b/common/test/run-bolt12_merkle-json.c
@@ -12,6 +12,7 @@
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>
#include <common/channel_type.h>
+#include <common/randbytes.h>
#include <common/setup.h>
/* AUTOGENERATED MOCKS START */
diff --git a/common/test/run-bolt12_merkle.c b/common/test/run-bolt12_merkle.c
index 43265ef..4d2f0bc 100644
--- a/common/test/run-bolt12_merkle.c
+++ b/common/test/run-bolt12_merkle.c
@@ -7,6 +7,7 @@
#include <ccan/array_size/array_size.h>
#include <common/channel_type.h>
#include <common/features.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <secp256k1_schnorrsig.h>
diff --git a/common/test/run-bolt12_period.c b/common/test/run-bolt12_period.c
index e4daf72..6b92f9c 100644
--- a/common/test/run-bolt12_period.c
+++ b/common/test/run-bolt12_period.c
@@ -5,6 +5,7 @@
#include <ccan/array_size/array_size.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>
+#include <common/randbytes.h>
#include <common/setup.h>
/* AUTOGENERATED MOCKS START */
diff --git a/common/test/run-channel_type.c b/common/test/run-channel_type.c
index 46edef1..2cac84a 100644
--- a/common/test/run-channel_type.c
+++ b/common/test/run-channel_type.c
@@ -2,6 +2,7 @@
#include "../channel_type.c"
#include "../features.c"
#include <ccan/tal/str/str.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
#include <lightningd/channel_state.h>
diff --git a/common/test/run-codex32.c b/common/test/run-codex32.c
index 3b32582..276bd02 100644
--- a/common/test/run-codex32.c
+++ b/common/test/run-codex32.c
@@ -8,6 +8,7 @@
#include <common/codex32.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <wire/wire.h>
diff --git a/common/test/run-coin_mvt.c b/common/test/run-coin_mvt.c
index e7512c0..99651cc 100644
--- a/common/test/run-coin_mvt.c
+++ b/common/test/run-coin_mvt.c
@@ -1,6 +1,7 @@
#include "config.h"
#include "../coin_mvt.c"
#include <ccan/tal/str/str.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
diff --git a/common/test/run-cryptomsg.c b/common/test/run-cryptomsg.c
index 9b7619d..0284f9d 100644
--- a/common/test/run-cryptomsg.c
+++ b/common/test/run-cryptomsg.c
@@ -1,5 +1,6 @@
#include "config.h"
#include <ccan/str/hex/hex.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <common/status.h>
#include <common/utils.h>
diff --git a/common/test/run-deprecation.c b/common/test/run-deprecation.c
index fe1a4cd..fa1378c 100644
--- a/common/test/run-deprecation.c
+++ b/common/test/run-deprecation.c
@@ -6,6 +6,7 @@ static const char *test_next_version;
#include "../deprecation.c"
#include <common/amount.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
#include <wire/wire.h>
diff --git a/common/test/run-derive_basepoints.c b/common/test/run-derive_basepoints.c
index 861a65d..cb896ad 100644
--- a/common/test/run-derive_basepoints.c
+++ b/common/test/run-derive_basepoints.c
@@ -5,6 +5,7 @@
#include <ccan/str/hex/hex.h>
#include <ccan/structeq/structeq.h>
#include <common/amount.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <common/utils.h>
#include <stdio.h>
diff --git a/common/test/run-features.c b/common/test/run-features.c
index 9d2d37d..efbea7f 100644
--- a/common/test/run-features.c
+++ b/common/test/run-features.c
@@ -3,6 +3,7 @@
#include "../memleak.c"
#include <bitcoin/script.h>
#include <ccan/mem/mem.h>
+#include <common/randbytes.h>
#include <common/setup.h>
/* AUTOGENERATED MOCKS START */
diff --git a/common/test/run-gossmap-fp16.c b/common/test/run-gossmap-fp16.c
index 274eccf..539b129 100644
--- a/common/test/run-gossmap-fp16.c
+++ b/common/test/run-gossmap-fp16.c
@@ -1,5 +1,6 @@
#include "config.h"
#include "../fp16.c"
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
#include <wire/wire.h>
diff --git a/common/test/run-htable.c b/common/test/run-htable.c
index 267ad8c..75164ce 100644
--- a/common/test/run-htable.c
+++ b/common/test/run-htable.c
@@ -8,6 +8,7 @@
#include <common/amount.h>
#include <common/memleak.h>
#include <common/pseudorand.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <common/utils.h>
#include <inttypes.h>
diff --git a/common/test/run-ip_port_parsing.c b/common/test/run-ip_port_parsing.c
index 026a06f..c7c5ba2 100644
--- a/common/test/run-ip_port_parsing.c
+++ b/common/test/run-ip_port_parsing.c
@@ -4,6 +4,7 @@
#include <stdio.h>
#include <common/amount.h>
+#include <common/randbytes.h>
#include <common/setup.h>
/* AUTOGENERATED MOCKS START */
diff --git a/common/test/run-json.c b/common/test/run-json.c
index 529d96c..3f1069e 100644
--- a/common/test/run-json.c
+++ b/common/test/run-json.c
@@ -5,6 +5,7 @@
#include <ccan/tal/str/str.h>
#include <common/channel_type.h>
#include <common/json_filter.h>
+#include <common/randbytes.h>
#include <common/sciddir_or_pubkey.h>
#include <common/setup.h>
#include <inttypes.h>
diff --git a/common/test/run-json_filter.c b/common/test/run-json_filter.c
index 54138cb..d682b82 100644
--- a/common/test/run-json_filter.c
+++ b/common/test/run-json_filter.c
@@ -6,6 +6,7 @@
#include "../json_stream.c"
#include <ccan/json_out/json_out.h>
#include <common/json_command.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
diff --git a/common/test/run-json_remove.c b/common/test/run-json_remove.c
index f1c16a2..8a15653 100644
--- a/common/test/run-json_remove.c
+++ b/common/test/run-json_remove.c
@@ -2,6 +2,7 @@
#include <common/amount.h>
#include <common/json_param.c>
#include <common/json_parse_simple.c>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
#include <wire/wire.h>
diff --git a/common/test/run-json_scan.c b/common/test/run-json_scan.c
index ab3e487..877bce6 100644
--- a/common/test/run-json_scan.c
+++ b/common/test/run-json_scan.c
@@ -2,6 +2,7 @@
#include "../json_parse.c"
#include "../json_parse_simple.c"
#include <common/amount.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
#include <wire/wire.h>
diff --git a/common/test/run-json_stream-filter.c b/common/test/run-json_stream-filter.c
index 1e484b4..6f6fe06 100644
--- a/common/test/run-json_stream-filter.c
+++ b/common/test/run-json_stream-filter.c
@@ -4,6 +4,7 @@
#include <assert.h>
#include <ccan/tal/str/str.h>
#include <common/channel_type.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <inttypes.h>
#include <stdio.h>
diff --git a/common/test/run-jsonrpc_io.c b/common/test/run-jsonrpc_io.c
index 4d5e604..d0f75ff 100644
--- a/common/test/run-jsonrpc_io.c
+++ b/common/test/run-jsonrpc_io.c
@@ -4,6 +4,7 @@
#include <ccan/io/io.h>
#include <common/amount.h>
#include <common/bigsize.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
#include <wire/wire.h>
diff --git a/common/test/run-key_derive.c b/common/test/run-key_derive.c
index f30fcdf..6b158f7 100644
--- a/common/test/run-key_derive.c
+++ b/common/test/run-key_derive.c
@@ -4,6 +4,7 @@
#include <assert.h>
#include <ccan/str/hex/hex.h>
#include <common/amount.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
#include <wire/wire.h>
diff --git a/common/test/run-lease_rates.c b/common/test/run-lease_rates.c
index 31da81c..f995eb8 100644
--- a/common/test/run-lease_rates.c
+++ b/common/test/run-lease_rates.c
@@ -1,6 +1,7 @@
#include "config.h"
#include "../amount.c"
#include "../lease_rates.c"
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
diff --git a/common/test/run-marginal_feerate.c b/common/test/run-marginal_feerate.c
index e0514cf..097852c 100644
--- a/common/test/run-marginal_feerate.c
+++ b/common/test/run-marginal_feerate.c
@@ -2,6 +2,7 @@
#include <assert.h>
#include <bitcoin/chainparams.h>
#include <common/amount.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
#include "../fee_states.c"
diff --git a/common/test/run-param.c b/common/test/run-param.c
index 749f6a4..1778942 100644
--- a/common/test/run-param.c
+++ b/common/test/run-param.c
@@ -8,6 +8,7 @@
#include <ccan/array_size/array_size.h>
#include <common/channel_type.h>
#include <common/memleak.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
diff --git a/common/test/run-psbt_diff.c b/common/test/run-psbt_diff.c
index 6d35569..235163f 100644
--- a/common/test/run-psbt_diff.c
+++ b/common/test/run-psbt_diff.c
@@ -1,4 +1,5 @@
#include "config.h"
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
#include "../amount.c"
diff --git a/common/test/run-route-infloop.c b/common/test/run-route-infloop.c
index 7556fd2..b52921e 100644
--- a/common/test/run-route-infloop.c
+++ b/common/test/run-route-infloop.c
@@ -8,6 +8,7 @@
#include <common/gossmap.h>
#include <common/gossip_store.h>
#include <common/memleak.h>
+#include <common/randbytes.h>
#include <common/route.h>
#include <common/sciddir_or_pubkey.h>
#include <common/setup.h>
diff --git a/common/test/run-route-specific.c b/common/test/run-route-specific.c
index 759b614..6e73f45 100644
--- a/common/test/run-route-specific.c
+++ b/common/test/run-route-specific.c
@@ -13,6 +13,7 @@
#include <common/gossmap.h>
#include <common/gossip_store.h>
#include <common/memleak.h>
+#include <common/randbytes.h>
#include <common/route.h>
#include <common/sciddir_or_pubkey.h>
#include <common/setup.h>
diff --git a/common/test/run-route.c b/common/test/run-route.c
index d9fb4d3..53c7359 100644
--- a/common/test/run-route.c
+++ b/common/test/run-route.c
@@ -6,6 +6,7 @@
#include <common/gossmap.h>
#include <common/gossip_store.h>
#include <common/memleak.h>
+#include <common/randbytes.h>
#include <common/route.h>
#include <common/sciddir_or_pubkey.h>
#include <common/setup.h>
diff --git a/common/test/run-route_blinding_test.c b/common/test/run-route_blinding_test.c
index 049e2a5..26b52e6 100644
--- a/common/test/run-route_blinding_test.c
+++ b/common/test/run-route_blinding_test.c
@@ -12,6 +12,7 @@
#include <common/channel_id.h>
#include <common/json_parse.h>
#include <common/json_stream.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <common/wireaddr.h>
#include <stdio.h>
diff --git a/common/test/run-shutdown_scriptpubkey.c b/common/test/run-shutdown_scriptpubkey.c
index db543a9..c98610d 100644
--- a/common/test/run-shutdown_scriptpubkey.c
+++ b/common/test/run-shutdown_scriptpubkey.c
@@ -4,6 +4,7 @@
#include <ccan/array_size/array_size.h>
#include <ccan/err/err.h>
#include <common/amount.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <common/utils.h>
#include <stdio.h>
diff --git a/common/test/run-splice_script.c b/common/test/run-splice_script.c
index c3bd63f..082f46c 100644
--- a/common/test/run-splice_script.c
+++ b/common/test/run-splice_script.c
@@ -6,6 +6,7 @@
#include "../json_stream.c"
#include "../json_parse_simple.c"
#include <bitcoin/chainparams.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <common/splice_script.h>
#include <assert.h>
diff --git a/common/test/run-tlv_span.c b/common/test/run-tlv_span.c
index 4055ec9..eb3ede4 100644
--- a/common/test/run-tlv_span.c
+++ b/common/test/run-tlv_span.c
@@ -2,6 +2,7 @@
#include "../bolt12.c"
#include "../bigsize.c"
#include "../../wire/fromwire.c"
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
diff --git a/common/test/run-tlv_unknown.c b/common/test/run-tlv_unknown.c
index fdecc46..071b104 100644
--- a/common/test/run-tlv_unknown.c
+++ b/common/test/run-tlv_unknown.c
@@ -5,6 +5,7 @@
#include "../../wire/towire.c"
#include "../bigsize.c"
#include "../bolt12.c"
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
diff --git a/common/test/run-version.c b/common/test/run-version.c
index 0e04b4c..3b46f56 100644
--- a/common/test/run-version.c
+++ b/common/test/run-version.c
@@ -1,9 +1,13 @@
#include "config.h"
#include "../version.c"
+#include <common/randbytes.h>
#include <common/setup.h>
#include <assert.h>
#include <stdio.h>
+/* AUTOGENERATED MOCKS START */
+/* AUTOGENERATED MOCKS END */
+
int main(int argc, char *argv[])
{
common_setup(argv[0]);
diff --git a/common/test/run-wireaddr.c b/common/test/run-wireaddr.c
index faa48e3..f5dd2ff 100644
--- a/common/test/run-wireaddr.c
+++ b/common/test/run-wireaddr.c
@@ -1,6 +1,7 @@
#include "config.h"
#include <bitcoin/chainparams.h>
#include <common/amount.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
diff --git a/connectd/connectd.c b/connectd/connectd.c
index 761b6dd..1b19b39 100644
--- a/connectd/connectd.c
+++ b/connectd/connectd.c
@@ -25,6 +25,7 @@
#include <common/ecdh_hsmd.h>
#include <common/gossmap.h>
#include <common/memleak.h>
+#include <common/randbytes.h>
#include <common/status.h>
#include <common/subdaemon.h>
#include <common/timeout.h>
@@ -1560,7 +1561,7 @@ setup_listeners(const tal_t *ctx,
if (sodium_mlock(&random, sizeof(random)) != 0)
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Could not lock the random prf key memory.");
- randombytes_buf((void * const)&random, 32);
+ randbytes((void * const)&random, 32);
/* generate static tor node address, take first 32 bytes from secret of node_id plus 32 random bytes from sodiom */
struct sha256 sha;
struct secret ss;
@@ -1585,7 +1586,7 @@ setup_listeners(const tal_t *ctx,
localaddr,
0);
/* get rid of blob data on our side of tor and add jitter */
- randombytes_buf((void * const)proposed_wireaddr[i].u.torservice.blob, TOR_V3_BLOBLEN);
+ randbytes((void * const)proposed_wireaddr[i].u.torservice.blob, TOR_V3_BLOBLEN);
if (!(proposed_listen_announce[i] & ADDR_ANNOUNCE)) {
continue;
diff --git a/connectd/handshake.c b/connectd/handshake.c
index 09e44bf..3382b50 100644
--- a/connectd/handshake.c
+++ b/connectd/handshake.c
@@ -8,6 +8,7 @@
#include <ccan/time/time.h>
#include <common/crypto_state.h>
#include <common/ecdh.h>
+#include <common/randbytes.h>
#include <common/status.h>
#include <common/utils.h>
#include <common/wireaddr.h>
@@ -15,7 +16,6 @@
#include <errno.h>
#include <secp256k1_ecdh.h>
#include <sodium/crypto_aead_chacha20poly1305.h>
-#include <sodium/randombytes.h>
#ifndef SUPERVERBOSE
#define SUPERVERBOSE(...)
@@ -199,7 +199,7 @@ static struct keypair generate_key(void)
struct keypair k;
do {
- randombytes_buf(k.priv.secret.data, sizeof(k.priv.secret.data));
+ randbytes(k.priv.secret.data, sizeof(k.priv.secret.data));
} while (!secp256k1_ec_pubkey_create(secp256k1_ctx,
&k.pub.pubkey, k.priv.secret.data));
return k;
diff --git a/connectd/test/run-initiator-success.c b/connectd/test/run-initiator-success.c
index 6a652ca..fd59a04 100644
--- a/connectd/test/run-initiator-success.c
+++ b/connectd/test/run-initiator-success.c
@@ -17,10 +17,10 @@
/* AUTOGENERATED MOCKS END */
/* No randomness please, we want to replicate test vectors. */
-#include <sodium/randombytes.h>
+#include <common/randbytes.h>
-static void seed_randomness(u8 *secret, size_t len);
-#define randombytes_buf(secret, len) seed_randomness((secret), (len))
+static void seed_randomness(u8 *secret, size_t len, u64 *offset);
+#define randbytes_(secret, len, offset) seed_randomness((secret), (len), (offset))
struct handshake;
static struct io_plan *test_write(struct io_conn *conn,
@@ -91,7 +91,7 @@ const void *trc;
static struct pubkey rs_pub, ls_pub, e_pub;
static struct privkey ls_priv, e_priv;
-static void seed_randomness(u8 *secret, size_t len)
+static void seed_randomness(u8 *secret, size_t len, u64 *offset)
{
assert(len == sizeof(e_priv));
memcpy(secret, &e_priv, len);
diff --git a/connectd/test/run-responder-success.c b/connectd/test/run-responder-success.c
index c4c2e73..9e93f80 100644
--- a/connectd/test/run-responder-success.c
+++ b/connectd/test/run-responder-success.c
@@ -17,10 +17,10 @@
/* AUTOGENERATED MOCKS END */
/* No randomness please, we want to replicate test vectors. */
-#include <sodium/randombytes.h>
+#include <common/randbytes.h>
-static void seed_randomness(u8 *secret, size_t len);
-#define randombytes_buf(secret, len) seed_randomness((secret), (len))
+static void seed_randomness(u8 *secret, size_t len, u64 *offset);
+#define randbytes_(secret, len, offset) seed_randomness((secret), (len), (offset))
struct handshake;
static struct io_plan *test_write(struct io_conn *conn,
@@ -90,7 +90,7 @@ extern secp256k1_context *secp256k1_ctx;
static struct pubkey ls_pub, e_pub;
static struct privkey ls_priv, e_priv;
-static void seed_randomness(u8 *secret, size_t len)
+static void seed_randomness(u8 *secret, size_t len, u64 *offset)
{
assert(len == sizeof(e_priv));
memcpy(secret, &e_priv, len);
diff --git a/hsmd/hsmd.c b/hsmd/hsmd.c
index 708fa84..8391164 100644
--- a/hsmd/hsmd.c
+++ b/hsmd/hsmd.c
@@ -17,6 +17,7 @@
#include <common/daemon_conn.h>
#include <common/hsm_secret.h>
#include <common/memleak.h>
+#include <common/randbytes.h>
#include <common/status.h>
#include <common/status_wiregen.h>
#include <common/subdaemon.h>
@@ -310,7 +311,7 @@ static void create_hsm(int fd, const char *passphrase)
/* Initialize wally tal context for libwally operations */
/* Generate random entropy for new mnemonic */
- randombytes_buf(entropy, sizeof(entropy));
+ randbytes(entropy, sizeof(entropy));
/* Generate mnemonic from entropy */
tal_wally_start();
diff --git a/lightningd/channel.c b/lightningd/channel.c
index 798c7ad..5e75455 100644
--- a/lightningd/channel.c
+++ b/lightningd/channel.c
@@ -4,6 +4,7 @@
#include <common/closing_fee.h>
#include <common/fee_states.h>
#include <common/json_command.h>
+#include <common/randbytes.h>
#include <common/wire_error.h>
#include <hsmd/hsmd_wiregen.h>
#include <lightningd/channel.h>
diff --git a/lightningd/invoice.c b/lightningd/invoice.c
index 49933a9..c609508 100644
--- a/lightningd/invoice.c
+++ b/lightningd/invoice.c
@@ -10,6 +10,7 @@
#include <common/bolt12_id.h>
#include <common/bolt12_merkle.h>
#include <common/json_command.h>
+#include <common/randbytes.h>
#include <common/random_select.h>
#include <common/timeout.h>
#include <hsmd/hsmd_wiregen.h>
@@ -20,7 +21,6 @@
#include <lightningd/peer_htlcs.h>
#include <lightningd/plugin_hook.h>
#include <lightningd/routehint.h>
-#include <sodium/randombytes.h>
#include <wallet/invoices.h>
#include <wallet/walletrpc.h>
@@ -1180,8 +1180,8 @@ static struct command_result *json_invoice(struct command *cmd,
info->payment_preimage = *preimage;
else
/* Generate random secret preimage. */
- randombytes_buf(&info->payment_preimage,
- sizeof(info->payment_preimage));
+ randbytes(&info->payment_preimage,
+ sizeof(info->payment_preimage));
/* Generate preimage hash. */
sha256(&rhash, &info->payment_preimage, sizeof(info->payment_preimage));
/* Generate payment secret. */
@@ -1620,7 +1620,7 @@ static void add_stub_blindedpath(const tal_t *ctx,
path = tal(NULL, struct blinded_path);
sciddir_or_pubkey_from_pubkey(&path->first_node_id, &ld->our_pubkey);
- randombytes_buf(&path_key, sizeof(path_key));
+ randbytes(&path_key, sizeof(path_key));
if (!pubkey_from_privkey(&path_key, &path->first_path_key))
abort();
path->path = tal_arr(path, struct blinded_path_hop *, 1);
diff --git a/onchaind/test/run-grind_feerate-bug.c b/onchaind/test/run-grind_feerate-bug.c
index ba71992..4e64c26 100644
--- a/onchaind/test/run-grind_feerate-bug.c
+++ b/onchaind/test/run-grind_feerate-bug.c
@@ -4,6 +4,7 @@
*/
#include "config.h"
#include <ccan/str/hex/hex.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <stdio.h>
@@ -171,6 +172,9 @@ void *notleak_(void *ptr UNNEEDED, bool plus_children UNNEEDED)
/* Generated stub for peer_billboard */
void peer_billboard(bool perm UNNEEDED, const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "peer_billboard called!\n"); abort(); }
+/* Generated stub for randbytes_ */
+void randbytes_(void *bytes UNNEEDED, size_t num_bytes UNNEEDED, u64 *offset UNNEEDED)
+{ fprintf(stderr, "randbytes_ called!\n"); abort(); }
/* Generated stub for shachain_get_secret */
bool shachain_get_secret(const struct shachain *shachain UNNEEDED,
u64 commit_num UNNEEDED,
diff --git a/plugins/fetchinvoice.c b/plugins/fetchinvoice.c
index a18b82e..64414b9 100644
--- a/plugins/fetchinvoice.c
+++ b/plugins/fetchinvoice.c
@@ -11,11 +11,11 @@
#include <common/json_stream.h>
#include <common/onion_message.h>
#include <common/overflows.h>
+#include <common/randbytes.h>
#include <inttypes.h>
#include <plugins/establish_onion_path.h>
#include <plugins/fetchinvoice.h>
#include <plugins/offers.h>
-#include <sodium.h>
static LIST_HEAD(sent_list);
@@ -452,7 +452,7 @@ static struct blinded_path *make_reply_path(const tal_t *ctx,
assert(tal_count(path) > 0);
- randombytes_buf(reply_secret, sizeof(struct secret));
+ randbytes(reply_secret, sizeof(struct secret));
if (sent->dev_reply_path) {
ids = sent->dev_reply_path;
@@ -1080,8 +1080,8 @@ struct command_result *json_fetchinvoice(struct command *cmd,
* bytes.
*/
invreq->invreq_metadata = tal_arr(invreq, u8, 16);
- randombytes_buf(invreq->invreq_metadata,
- tal_bytelen(invreq->invreq_metadata));
+ randbytes(invreq->invreq_metadata,
+ tal_bytelen(invreq->invreq_metadata));
}
}
@@ -1589,7 +1589,7 @@ struct command_result *json_sendinvoice(struct command *cmd,
* - MUST set `invoice_payment_hash` to the SHA256 hash of the
* `payment_preimage` that will be given in return for payment.
*/
- randombytes_buf(&sent->inv_preimage, sizeof(sent->inv_preimage));
+ randbytes(&sent->inv_preimage, sizeof(sent->inv_preimage));
sent->inv->invoice_payment_hash = tal(sent->inv, struct sha256);
sha256(sent->inv->invoice_payment_hash,
&sent->inv_preimage, sizeof(sent->inv_preimage));
diff --git a/plugins/keysend.c b/plugins/keysend.c
index 58c66cb..2e1ba29 100644
--- a/plugins/keysend.c
+++ b/plugins/keysend.c
@@ -7,10 +7,10 @@
#include <common/json_param.h>
#include <common/json_stream.h>
#include <common/memleak.h>
+#include <common/randbytes.h>
#include <inttypes.h>
#include <plugins/channel_hint.h>
#include <plugins/libplugin-pay.h>
-#include <sodium.h>
#define PREIMAGE_TLV_TYPE 5482373484
#define KEYSEND_FEATUREBIT 55
@@ -49,7 +49,7 @@ static struct keysend_data *keysend_init(struct payment *p)
* and populate the preimage field in the keysend_data and the
* payment_hash in the payment. */
d = tal(p, struct keysend_data);
- randombytes_buf(&d->preimage, sizeof(d->preimage));
+ randbytes(&d->preimage, sizeof(d->preimage));
ccan_sha256(&payment_hash, &d->preimage, sizeof(d->preimage));
p->payment_hash = tal_dup(p, struct sha256, &payment_hash);
d->extra_tlvs = NULL;
diff --git a/plugins/offers_invreq_hook.c b/plugins/offers_invreq_hook.c
index 153def6..16dd717 100644
--- a/plugins/offers_invreq_hook.c
+++ b/plugins/offers_invreq_hook.c
@@ -11,10 +11,10 @@
#include <common/json_stream.h>
#include <common/onion_message.h>
#include <common/overflows.h>
+#include <common/randbytes.h>
#include <inttypes.h>
#include <plugins/offers.h>
#include <plugins/offers_invreq_hook.h>
-#include <sodium.h>
/* We need to keep the reply path around so we can reply with invoice */
struct invreq {
@@ -1006,7 +1006,7 @@ static struct command_result *listoffers_done(struct command *cmd,
* - MUST set `invoice_payment_hash` to the SHA256 hash of the
* `payment_preimage` that will be given in return for payment.
*/
- randombytes_buf(&ir->preimage, sizeof(ir->preimage));
+ randbytes(&ir->preimage, sizeof(ir->preimage));
ir->inv->invoice_payment_hash = tal(ir->inv, struct sha256);
sha256(ir->inv->invoice_payment_hash,
&ir->preimage, sizeof(ir->preimage));
diff --git a/plugins/offers_offer.c b/plugins/offers_offer.c
index 34b6844..53c88f0 100644
--- a/plugins/offers_offer.c
+++ b/plugins/offers_offer.c
@@ -9,9 +9,9 @@
#include <common/json_stream.h>
#include <common/onion_message.h>
#include <common/overflows.h>
+#include <common/randbytes.h>
#include <plugins/offers.h>
#include <plugins/offers_offer.h>
-#include <sodium/randombytes.h>
static bool msat_or_any(const char *buffer,
const jsmntok_t *tok,
@@ -706,8 +706,8 @@ struct command_result *json_invoicerequest(struct command *cmd,
* - MUST set `invreq_metadata` to an unpredictable series of bytes.
*/
invreq->invreq_metadata = tal_arr(invreq, u8, 16);
- randombytes_buf(invreq->invreq_metadata,
- tal_bytelen(invreq->invreq_metadata));
+ randbytes(invreq->invreq_metadata,
+ tal_bytelen(invreq->invreq_metadata));
/* BOLT #12:
* - otherwise (not responding to an offer):
diff --git a/plugins/renepay/test/run-bottleneck.c b/plugins/renepay/test/run-bottleneck.c
index 6e8bda7..ec1e934 100644
--- a/plugins/renepay/test/run-bottleneck.c
+++ b/plugins/renepay/test/run-bottleneck.c
@@ -14,9 +14,9 @@
#include <bitcoin/chainparams.h>
#include <bitcoin/preimage.h>
#include <ccan/str/hex/hex.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <common/utils.h>
-#include <sodium/randombytes.h>
/* AUTOGENERATED MOCKS START */
/* AUTOGENERATED MOCKS END */
@@ -231,7 +231,7 @@ int main(int argc, char *argv[])
pinfo.base_prob_success = 1.0;
pinfo.use_shadow = false;
- randombytes_buf(&preimage, sizeof(preimage));
+ randbytes(&preimage, sizeof(preimage));
sha256(&pinfo.payment_hash, &preimage, sizeof(preimage));
// char hex_preimage[600], hex_sha256[600];
diff --git a/tests/fuzz/connectd_handshake.h b/tests/fuzz/connectd_handshake.h
index 139ee25..8145bd6 100644
--- a/tests/fuzz/connectd_handshake.h
+++ b/tests/fuzz/connectd_handshake.h
@@ -1,6 +1,6 @@
/* This header contains globals and helper functions used by all the
* fuzz-connectd-handshake-act* fuzz targets. It also takes care of intercepting
- * io_read(), io_write(), and randombytes_buf(), so that the actual fuzz targets
+ * io_read(), io_write(), and randbytes(), so that the actual fuzz targets
* only need to implement the test_read() and test_write() interceptors and the
* run() function.
*/
@@ -11,6 +11,7 @@
#include <assert.h>
#include <ccan/io/io.h>
#include <ccan/str/hex/hex.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <common/status.h>
#include <common/wireaddr.h>
@@ -19,9 +20,9 @@
/* No randomness please, we want to replicate test vectors. */
#include <sodium/randombytes.h>
-
+#undef randbytes
static void seeded_randombytes_buf(u8 *secret, size_t len);
-#define randombytes_buf(secret, len) seeded_randombytes_buf((secret), (len))
+#define randbytes(secret, len) seeded_randombytes_buf((secret), (len))
struct handshake;
diff --git a/wire/test/run-peer-wire.c b/wire/test/run-peer-wire.c
index f8b17c3..a220af8 100644
--- a/wire/test/run-peer-wire.c
+++ b/wire/test/run-peer-wire.c
@@ -13,6 +13,7 @@
#include <common/channel_type.h>
#include <common/memleak.h>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <common/sphinx.h>
#include <common/wireaddr.h>
@@ -21,6 +22,9 @@
extern secp256k1_context *secp256k1_ctx;
/* AUTOGENERATED MOCKS START */
+/* Generated stub for randbytes_ */
+void randbytes_(void *bytes UNNEEDED, size_t num_bytes UNNEEDED, u64 *offset UNNEEDED)
+{ fprintf(stderr, "randbytes_ called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */
/* memsetting pubkeys doesn't work */
diff --git a/wire/test/run-tlvstream.c b/wire/test/run-tlvstream.c
index 2cb0c8c..3782191 100644
--- a/wire/test/run-tlvstream.c
+++ b/wire/test/run-tlvstream.c
@@ -11,6 +11,7 @@ static const char *reason;
#include <common/channel_type.h>
#include <common/memleak.h>
#include <common/node_id.c>
+#include <common/randbytes.h>
#include <common/setup.h>
#include <wire/peer_wiregen.c>
@@ -29,6 +30,9 @@ int chainparams_get_ln_port(const struct chainparams *params UNNEEDED)
bool fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "fromwire_channel_id called!\n"); abort(); }
+/* Generated stub for randbytes_ */
+void randbytes_(void *bytes UNNEEDED, size_t num_bytes UNNEEDED, u64 *offset UNNEEDED)
+{ fprintf(stderr, "randbytes_ called!\n"); abort(); }
/* Generated stub for towire_channel_id */
void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "towire_channel_id called!\n"); abort(); }
Why this scored 19/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.