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

sys/random: default to musl LCG instead of TinyMT #17188

Merged
merged 2 commits into from Nov 30, 2021

Conversation

benpicco
Copy link
Contributor

@benpicco benpicco commented Nov 11, 2021

Contribution description

In A Guideline on Pseudorandom Number Generation (PRNG) in the IoT the paper concludes with

The Knuth LCG is the most efficient general purpose generator that provides decent statistical quality.
It is simple and lean enough to run on very constrained devices.

So let's select prng_musl_lcg to be the default PRNG instead of prng_tinymt32.

Testing procedure

This saves a good chunk of memory on e.g. samr21-xpro:

prng_tinymt32

   text	   data	    bss	    dec	    hex	filename
  26452	    136	   2824	  29412	   72e4	tests/rng/bin/samr21-xpro/tests_rng.elf

prng_musl_lcg

   text	   data	    bss	    dec	    hex	filename
  26208	    136	   2808	  29152	   71e0	tests/rng/bin/samr21-xpro/tests_rng.elf

Issues/PRs references

In [0] the paper concludes with

> The Knuth LCG is the most efficient general purpose generator that
> provides decent statistical quality.
> It is simple and lean enough to run on very constrained devices.

So let's select `prng_musl_lcg` to be the default PRNG instead of
`prng_tinymt32`.

This gives a good chunk of memory on e.g. `samr21-xpro`:

prng_tinymt32
-------------

   text	   data	    bss	    dec	    hex	filename
  26452	    136	   2824	  29412	   72e4	tests/rng/bin/samr21-xpro/tests_rng.elf

prng_musl_lcg
-------------

   text	   data	    bss	    dec	    hex	filename
  26208	    136	   2808	  29152	   71e0	tests/rng/bin/samr21-xpro/tests_rng.elf

[0] https://sci-hub.se/10.1145/3453159
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Nov 11, 2021
@kaspar030
Copy link
Contributor

@PeterKietzmann do you remember why we didn't do this after you published the paper?

@PeterKietzmann
Copy link
Member

PeterKietzmann commented Nov 12, 2021

@PeterKietzmann do you remember why we didn't do this after you published the paper?

No particular reason. I want to restructure the random subsystem and started from the bottom #14324, currently waiting for #15671. Once I made it to the PRNG level, the plan was to set the default generators.

I'm fine with changing now.

@benpicco
Copy link
Contributor Author

Looks like all applications that already have been converted to Kconfig and use random select

CONFIG_MODULE_PRNG_TINYMT32=y

Is this really needed? Can't we have a default PRNG with Kconfig?

@leandrolanzieri
Copy link
Contributor

Looks like all applications that already have been converted to Kconfig and use random select

CONFIG_MODULE_PRNG_TINYMT32=y

Is this really needed? Can't we have a default PRNG with Kconfig?

There are defaults, in fact you are modifying one here. The problem is that the makefile dependency resolution fails to default to use HWRNG (as you pointed in #16732), which Kconfig prioritizes. We decided to override this in application configuration and leave it 'well modelled'.

@leandrolanzieri
Copy link
Contributor

Looks like all applications that already have been converted to Kconfig and use random select

CONFIG_MODULE_PRNG_TINYMT32=y

Is this really needed? Can't we have a default PRNG with Kconfig?

BTW you'll also need to adapt those app.config.test to match the new Makefile default:

CONFIG_MODULE_PRNG_MUSL_LCG=y

@github-actions github-actions bot added the Area: tests Area: tests and testing framework label Nov 12, 2021
@benpicco
Copy link
Contributor Author

Hm so maybe we should remove the HWRNG default with Kconfig to make it even with the make based dependency system?

@PeterKietzmann
Copy link
Member

Hm so maybe we should remove the HWRNG default with Kconfig to make it even with the make based dependency system?

+1. Besides harmonizing both systems, HWRNG isn't always the best choice for direct random number generation.

@leandrolanzieri
Copy link
Contributor

Hm so maybe we should remove the HWRNG default with Kconfig to make it even with the make based dependency system?

+1. Besides harmonizing both systems, HWRNG isn't always the best choice for direct random number generation.

Ok, if you think that's the best default sure. I would not do it just for the fact of matching the makefile default though.

@benpicco
Copy link
Contributor Author

The random dependency resolution is such a mess unfortunately. The reason why I went with HWRNG by default was because that was the only way to get different randomness across reboots (important for CoAP as we otherwise always start with the same sequence number and the server discards our requests).

It's also much slimmer than TinyMT, but with LCG that is not so relevant anymore.

Ideally we'd default to HWRNG to seed the PRNG, if no HWRNG is available use PUF SRAM to at least get some random seed.
But I don't think this is possible with the current make based dependency system.

Kconfig of course will allow us to model this behavior properly.

@PeterKietzmann
Copy link
Member

Seeding needs rework. #15671 is the next step towards seed generation and separation.

@@ -3,5 +3,5 @@ CONFIG_MODULE_RANDOM=y
# Should be autoselecting the MODULE_PRNG_HWRNG if possible
# Since the makefile cannot we have to override until end of migration
# Remove when TEST_KCONFIG is complete
CONFIG_MODULE_PRNG_TINYMT32=y
CONFIG_MODULE_PRNG_MUSL_LCG=y
Copy link
Member

Choose a reason for hiding this comment

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

Neither Tinymt32, nor LCG are crypto RNGs. One step further, HWRNGs are not necessarily secure for direct use. Hence, we should default to a CSPRNG in crypto contexts. SHA256PRNG would be the leanest choice.

This shows the problem with our current random module: There is only a single (non-secure) default RNG. As said, I am planning to continue my work on that starting from #15671.

Copy link
Member

Choose a reason for hiding this comment

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

What I meant: crypto packages (cifra, hacl, dsa, ...) that require access to a random number generator should configure a crypto RNG (SHA256PRNG)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, but let's do this as a follow-up. This PR only changes the default RNG, those apps are still using the default RNG - if they should not, that would be a different PR.

Copy link
Member

Choose a reason for hiding this comment

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

What if we say "set correct default choice"? Anyway, we can also go with two PRs.

@fjmolinas fjmolinas added this to the Release 2022.01 milestone Nov 18, 2021
@benpicco benpicco added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Nov 18, 2021
@PeterKietzmann
Copy link
Member

Here?

@PeterKietzmann PeterKietzmann added Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines labels Nov 24, 2021
child.expect("1918982324")
child.expect("1550167154")
child.expect("3454972398")
child.expect("1034066532")
Copy link
Member

Choose a reason for hiding this comment

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

How about moving this to a separate test case?

@PeterKietzmann
Copy link
Member

  • tests/rng make test passes with native and nrf52840dk (selects Musl LCG).
  • checked some tests/pkg_* (cifra, libcose, c25519,monocipher) with native or nrf52040dk, all passand use Musl LCG.
  • menuconfig shows the correct defaults (tests/pkg_c25519 in the screenshot
    Bildschirmfoto_2021-11-29_09-44-43
    )

@benpicco did you create the new test values of took them from somewhere? In the latter case, is there more?

@PeterKietzmann PeterKietzmann added the Reviewed: 3-testing The PR was tested according to the maintainer guidelines label Nov 29, 2021
@benpicco
Copy link
Contributor Author

The new test values are just the output of the new RNG

@PeterKietzmann
Copy link
Member

So lets fix this occurce, merge, and fix the default choice for crypto packages afterwards

@kaspar030 kaspar030 removed the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Nov 30, 2021
@kaspar030 kaspar030 added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Nov 30, 2021
@benpicco benpicco merged commit 2079642 into RIOT-OS:master Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: build system Area: Build system Area: Kconfig Area: Kconfig integration Area: sys Area: System Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 3-testing The PR was tested according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants