Skip to content

Conversation

@tnull
Copy link
Collaborator

@tnull tnull commented Oct 29, 2025

We bump our rand dependency to the latest stable version.

Additionally, we now use os_rng for seed/mnemonic generation. While the previously-used thread_rng should be totally fine, but os_rng is guaranteed to block until there is sufficient entropy available (e.g., after startup), which might slightly improve security here.

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Oct 29, 2025

👋 Thanks for assigning @tankyleo as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@tnull tnull requested a review from tankyleo October 29, 2025 08:05
@tnull tnull force-pushed the 2025-10-use-os-rng branch from f139d39 to 42b7d21 Compare October 29, 2025 08:19
tnull added 2 commits October 29, 2025 09:24
We bump our rand dependency to the latest stable version.
The previously-used `thread_rng` should be fine, but `os_rng` is
guaranteed to block until there is sufficient entropy available (e.g.,
after startup), which might slightly improve security here.
@tnull tnull force-pushed the 2025-10-use-os-rng branch from 42b7d21 to e173997 Compare October 29, 2025 08:24
@tnull
Copy link
Collaborator Author

tnull commented Oct 29, 2025

Flaky CI is unrelated.

Copy link

@tankyleo tankyleo left a comment

Choose a reason for hiding this comment

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

Thank you !

impl EntropySource for RandEntropySource {
fn fill_bytes(&self, buffer: &mut [u8]) {
rand::thread_rng().fill_bytes(buffer);
rand::rng().fill_bytes(buffer);

Choose a reason for hiding this comment

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

In the next commit, I would have used OsRng here too since it's used for cryptographic nonces.

Choose a reason for hiding this comment

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

I am conscious of the performance tradeoff here as this is a frequent operation, maybe OsRng would be overkill ? Cryptographic nonces have stronger requirements than the identifiers we use ThreadRng for elsewhere

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that's why I kept it with the thread RNG, which should be perfectly fine for cryptographic operations. I guess if we'd want to be super cautious we could look into periodic re-seeding via ReseedingRng, but not even sure if that is warranted. WDYT?

Choose a reason for hiding this comment

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

Looked into the current reseed behaviour, it's already automatically called every 64kB, I think we are good there.

They do recommend to explicitly reseed on any fork, but I don't think we ever fork right ? If so I think I am good with the current choice.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They do recommend to explicitly reseed on any fork, but I don't think we ever fork right ? If so I think I am good with the current choice.

Yeah, was also tripping over that and considered reseeding on any new thread spawned by tokio. But, IIUC, that should happen automatically in the rng() call.

OsRng.try_fill_bytes(&mut key).map_err(|e| {
log_error!(logger, "Failed to generate entropy: {}", e);
std::io::Error::new(std::io::ErrorKind::Other, "Failed to generate seed bytes")
})?;

Choose a reason for hiding this comment

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

If we propagate the Err from the OsRng here should we also propagate the Err in generate_entropy_mnemonic above ?

Choose a reason for hiding this comment

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

I guess I am looking for consistency here, but maybe the difference is a public API function vs a function internal to the crate ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we propagate the Err from the OsRng here should we also propagate the Err in generate_entropy_mnemonic above ?

Well, we would need to introduce an enum error type just for this case, which seems like overkill. It could even be argued that we should to panic in this case, as according to the docs of OsRng failing is only expected in case of system misconfiguration and we fail to actually provide sufficient entropy, i.e., every operation after this could be considered unsafe.

Choose a reason for hiding this comment

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

hmm ok would you consider panicking here ? sounds to me like OsRng returning an Error is very rare, but if it ever does, user should probably stop what they are doing and consider running ldk-node on a different machine.

Copy link
Collaborator Author

@tnull tnull Oct 31, 2025

Choose a reason for hiding this comment

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

hmm ok would you consider panicking here ? sounds to me like OsRng returning an Error is very rare, but if it ever does, user should probably stop what they are doing and consider running ldk-node on a different machine.

Well, the other path will happen on build only, so instead of panicking we'd just be erroring out before we did anything really.

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