Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jitterentropy as RNG #4325

Merged
merged 2 commits into from
Oct 10, 2024
Merged

Add jitterentropy as RNG #4325

merged 2 commits into from
Oct 10, 2024

Conversation

ghost
Copy link

@ghost ghost commented Aug 24, 2024

Add jitterentropy-library to Botan as a module. When enabled, the system RNG can use this additional entropy and depends on jitterentropy-library.

So far I have implemented this only for BOTAN_TARGET_OS_HAS_CCRANDOM for Botan 3, which got configured fairly automatically for my macOS machine.

If this is something that is useful for Botan. I can easily provide implementation for other systems that I have access to, (windows and ubuntu, plus macOS/Android with arc4random). Plus port it to the Botan 2 branch.

I did not format system_rng.cpp yet because most changes would be in existing code (using vs code which should pick up .clang-format).

There is an integration project (using submodules for Botan and the jitter library), in case this is of use for someone. It probably works on linux too, but I'm not sure it the jitter will actually be used there because of the usage of BOTAN_TARGET_OS_HAS_CCRANDOM. At least with tests for Botan 2, a WSL ubuntu picked BOTAN_TARGET_OS_HAS_GETRANDOM here for System_RNG_Impl.

Copy link
Owner

@randombit randombit left a comment

Choose a reason for hiding this comment

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

So some significant skepticism here (ala #4309) but at least this is included in a few widely used Linux distros, so I'm relatively open to merging something that supports this source but I would be quite against doing so where the output of jitter is xored with the system RNG. This should either be its own RNG type, that is used if the application specifically wants to use it, or (I think much more appropriately) it should be an EntropySource that is used to feed userspace RNGs.

src/lib/jitter/info.txt Outdated Show resolved Hide resolved
src/lib/jitter/jitter.cpp Outdated Show resolved Hide resolved
src/lib/jitter/jitter.cpp Outdated Show resolved Hide resolved
src/lib/jitter/jitter.cpp Outdated Show resolved Hide resolved
src/lib/jitter/jitter.h Outdated Show resolved Hide resolved
src/lib/rng/system_rng/system_rng.cpp Outdated Show resolved Hide resolved
src/lib/jitter/info.txt Outdated Show resolved Hide resolved
src/lib/jitter/info.txt Outdated Show resolved Hide resolved
src/lib/jitter/jitter.cpp Outdated Show resolved Hide resolved
src/lib/jitter/jitter.cpp Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Aug 26, 2024

Coverage Status

coverage: 90.985% (-0.02%) from 91.004%
when pulling 5323fa8 on plancksecurity:jitter
into 1fe3150 on randombit:master.

@ghost ghost marked this pull request as draft August 27, 2024 06:34
@ghost ghost changed the title Add jitter entropy to the System RNG (BOTAN_TARGET_OS_HAS_CCRANDOM) Add jitterentropy as RNG Sep 1, 2024
@ghost ghost force-pushed the jitter branch 3 times, most recently from 1e9180c to d56064d Compare September 1, 2024 08:51
@ghost
Copy link
Author

ghost commented Sep 1, 2024

I rewrote this as an RNG and implemented all feedback points, plus a very basic test to make sure code gets executed. Please re-check and let me know.

@ghost ghost marked this pull request as ready for review September 1, 2024 08:55
@ghost ghost requested a review from randombit September 1, 2024 08:56
Copy link
Owner

@randombit randombit left a comment

Choose a reason for hiding this comment

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

OK looking better now, thanks for addressing the previous round. Some additional comments outside of the changed code so GH won’t let me comment

  • It might be useful to (also) expose Jitter_RNG as an entropy source, in the similar way as we do for System_RNG and Processor_RNG
  • Please also update the rng documentation
  • Depending on your use case you may want to also expose this to the C wrapper in botan_rng_init

src/lib/rng/jitter_rng/jitter.h Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.h Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter.cpp Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter.cpp Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.h Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter.cpp Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter.cpp Outdated Show resolved Hide resolved
src/tests/test_jitter_rng.cpp Outdated Show resolved Hide resolved
@ghost
Copy link
Author

ghost commented Sep 7, 2024

I have addressed all issues except:

  • Formatting (I'm using clang-format but it looks like the CI check is using different parameters, and I cannot reproduce that format, even using clang-format-17)
  • Entropy source
  • FFI (DONE)

Will tackle those next.

@ghost ghost requested a review from randombit September 8, 2024 09:13
@ghost
Copy link
Author

ghost commented Sep 8, 2024

I have not figured out the formatting issue. If you have a script somewhere in the repo, please tell.
I responded to every feedback point, please let me know if I misunderstood. There's now a jitter RNG and jitter entropy source.

src/lib/rng/jitter_rng/jitter_rng.cpp Outdated Show resolved Hide resolved
src/lib/entropy/entropy_srcs.cpp Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.cpp Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.h Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.cpp Outdated Show resolved Hide resolved
@ghost

This comment was marked as resolved.

@ghost ghost requested a review from randombit September 11, 2024 11:34
@ghost

This comment was marked as resolved.

@ghost ghost force-pushed the jitter branch 3 times, most recently from c7cb815 to d7754ed Compare September 21, 2024 07:26
@ghost
Copy link
Author

ghost commented Sep 21, 2024

Please (re-)review.

src/lib/rng/jitter_rng/jitter_rng.h Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.h Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.cpp Outdated Show resolved Hide resolved
Copy link
Collaborator

@reneme reneme left a comment

Choose a reason for hiding this comment

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

Here's another round of polishing. Along with a few nits and style/consistency suggestions, I have a left an idea for compacting/improving the test code.

We should also enable this module in at least a few CI targets to ensure sustainability. Since it introduces a new (optional) dependency it'll require a bit of CI script mangling. No worries, I'll send you a patch for it next week.

news.rst Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.cpp Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.cpp Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.cpp Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.cpp Outdated Show resolved Hide resolved
src/tests/test_jitter_rng.cpp Outdated Show resolved Hide resolved
src/tests/test_jitter_rng.cpp Outdated Show resolved Hide resolved
src/tests/test_jitter_rng.cpp Outdated Show resolved Hide resolved
src/tests/test_jitter_rng.cpp Outdated Show resolved Hide resolved
src/tests/test_jitter_rng.cpp Outdated Show resolved Hide resolved
@dirkz
Copy link
Contributor

dirkz commented Oct 5, 2024

Thank you very much for the great feedback, I have pushed a new update. Especially the declaration/definition of Jitter_RNG_Internal may require another look. Please let me know.

@Delta-dev-99
Copy link
Contributor

About the declaration/definition of Jitter_RNG_Internal:

Using anonymous namespace in headers is strongly discouraged. It generates a warning on my computer (Ubuntu x86_64 using GCC 13.2). I don't think it's harmful in this case because there's only a forward declaration in there but I don't see any benefits either. The default visibility is already hidden.

Personally I think the best would be to just use an attribute to hide the nested internal class declaration, but this will probably not work on windows because there's no way to disable __dclspec(dllexport) of a nested definition of an exported class. I'm not sure if __dclspec(dllexport) propagates to nested type definitions though. I will test it.

Considering this, the best compromise will probably be to just make the internal class a standalone declaration. It won't generate symbols and we'll have to deal with a bit of namespace pollution.

Copy link
Collaborator

@reneme reneme left a comment

Choose a reason for hiding this comment

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

A few more minor nits. Most notably, the BOTAN_ASSERT suggestions are purely optional, in my opinion.

Also a suggestion for an implementation of RandomNumberGenerator::clear() that might (or might not) make sense for this adapter.

Generally, I think this converged nicely! Thanks for your patience. :)

src/lib/rng/jitter_rng/jitter_rng.cpp Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.h Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.h Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.cpp Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.cpp Outdated Show resolved Hide resolved
src/lib/rng/jitter_rng/jitter_rng.h Outdated Show resolved Hide resolved
doc/api_ref/rng.rst Outdated Show resolved Hide resolved
src/tests/test_jitter_rng.cpp Outdated Show resolved Hide resolved
src/tests/test_jitter_rng.cpp Outdated Show resolved Hide resolved
src/tests/test_jitter_rng.cpp Show resolved Hide resolved
@dirkz dirkz force-pushed the jitter branch 2 times, most recently from 6aafa62 to 57ccd78 Compare October 8, 2024 18:39
@dirkz
Copy link
Contributor

dirkz commented Oct 8, 2024

Again, thank you very much for your feedback. I have implemented all of it. Please re-check.

Copy link
Collaborator

@reneme reneme 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! This looks good now.

Still to do on our side:

  • CI integration (probably for the coverage, valgrind and sanitizer builds ?)

On the note of CI: jitterentropy-library (standalone, not the daemon) doesn't seem to exist as a package for ubuntu. Or am I missing something? For testing locally, I built it from source. This isn't a big deal as the library is small, but I'd like to omit this on CI if possible.

src/tests/test_jitter_rng.cpp Show resolved Hide resolved
@reneme
Copy link
Collaborator

reneme commented Oct 9, 2024

Regarding CI: Here a patch that enables running your test in (some) CI configurations: reneme@f22d6b8
You might need to rebase to the latest master branch before this applies. Also note: #4325 (comment)
Please cherry-pick my patch and update this pull request.

@randombit I had to build jitterentropy from source, because Ubuntu doesn't provide a package for it. 😞

Currently Ubuntu does not ship with a package for the
jitterentropy-library. Therefore, this pulls the sources from the
maintainer's GitHub and builds/installs it from source.

The library is tiny, so there's no significant overhead in time, but of
course the additional build dependency on another repository is a
bummer.
@dirkz
Copy link
Contributor

dirkz commented Oct 9, 2024

Regarding CI: Here a patch that enables running your test in (some) CI configurations: reneme@f22d6b8 You might need to rebase to the latest master branch before this applies. Also note: #4325 (comment) Please cherry-pick my patch and update this pull request.

Rebased to current master and your CI change cherry-picked.

Copy link
Owner

@randombit randombit left a comment

Choose a reason for hiding this comment

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

LGTM

@reneme
Copy link
Collaborator

reneme commented Oct 9, 2024

@randombit Your call regarding merging before or after the 3.6.0 release. Given that this is entirely inside an optional module, there's probably little chance for general surprises here.

@randombit
Copy link
Owner

Seems low risk to me so fine for 3.6

@randombit randombit merged commit 0d8c1dd into randombit:master Oct 10, 2024
40 checks passed
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.

5 participants