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

dev_random: initial import of a high-level hwnrg API #5153

Closed
wants to merge 4 commits into from

Conversation

miri64
Copy link
Member

@miri64 miri64 commented Mar 23, 2016

A shot at fixing #5150 (name up to discussion).

This modules requires the board to define a randomness source DEV_RANDOM_SRC in its board.h (DEV_RANDOM_SRC_PERIPH_HWRNG, i.e. periph_hwrng is the default). Since this module is purely macro-based it is easy to override for other DEV_RANDOM_SRC cases (e.g. the functionality in #4989). I added the ADC as another example.

Depending on #4989.

@miri64 miri64 added Type: new feature The issue requests / The PR implemements a new feature for RIOT Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR labels Mar 23, 2016
@miri64 miri64 added this to the Release 2016.07 milestone Mar 23, 2016
@kYc0o
Copy link
Contributor

kYc0o commented Jul 22, 2016

This new feature requires more discussion, as other related PRs about RNG. Postponed.

@kYc0o kYc0o modified the milestones: Release 2016.10, Release 2016.07 Jul 22, 2016
@miri64
Copy link
Member Author

miri64 commented Sep 1, 2016

I'd like to have some input. Especially e.g. from @kaspar030.

@miri64 miri64 force-pushed the dev_random/api/initial branch 2 times, most recently from 7e26747 to 983a3d2 Compare September 1, 2016 18:20
@miri64 miri64 added the State: waiting for other PR State: The PR requires another PR to be merged first label Sep 1, 2016
@miri64
Copy link
Member Author

miri64 commented Sep 1, 2016

Added @mtausig's #4989 as a dependency.

@miri64 miri64 force-pushed the dev_random/api/initial branch 3 times, most recently from 22e1ab5 to fcb7e85 Compare September 1, 2016 18:41
@kaspar030
Copy link
Contributor

Why did you choose to use function macros?

@miri64
Copy link
Member Author

miri64 commented Sep 1, 2016

To be perfectly honest... I'm not sure anymore :-/. I guess to make absolutely clear that these just wrap around some kind of hardware access. But I'm fine with changing it to inline functions, if that is better suited.

@miri64
Copy link
Member Author

miri64 commented Sep 1, 2016

Moved to a inline function based API.

@miri64
Copy link
Member Author

miri64 commented Sep 1, 2016

Added config for hwnrg boards, too. For some reason I did not find them earlier, when I did the at86rf2xx boards. I need to research the behavior of adc_init() before I include the rest of the boards that havn't a source already.

@miri64 miri64 force-pushed the dev_random/api/initial branch from 47b924f to ed98b73 Compare September 2, 2016 09:09
@miri64
Copy link
Member Author

miri64 commented Sep 2, 2016

Rebased and squashed.

@miri64 miri64 removed the State: waiting for other PR State: The PR requires another PR to be merged first label Sep 2, 2016
@miri64
Copy link
Member Author

miri64 commented Sep 2, 2016

(#4989 was merged)

@miri64 miri64 force-pushed the dev_random/api/initial branch from ed98b73 to 92e2c50 Compare September 26, 2016 13:58
@miri64 miri64 added the Community: Hack'n'ACK candidate This PR is a candidate for review and discussion during one of RIOT's monthly Hack'n'ACK parties label Sep 26, 2016
@miri64
Copy link
Member Author

miri64 commented Sep 26, 2016

Prepared for review tomorrow ;-)

@mtausig
Copy link
Contributor

mtausig commented Sep 26, 2016

Will there be some Makefile flag, to make a project depend onto the availability of a random source? I mean somethin like we have now in FEATURE_REQUIRED = periph_hwrng.

@miri64 miri64 added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Sep 27, 2016
@miri64
Copy link
Member Author

miri64 commented Oct 4, 2016

Will there be some Makefile flag, to make a project depend onto the availability of a random source? I mean somethin like we have now in FEATURE_REQUIRED = periph_hwrng.

@mtausig the randomness source would be chosen by the board's configuration as I already prepared it for the boards it applies for. We can make this configurable, but I don't see the use case right now.

@mtausig
Copy link
Contributor

mtausig commented Oct 4, 2016

On Die, 2016-10-04 at 09:33 -0700, Martine Lenders wrote:

Will there be some Makefile flag, to make a project depend onto the
availability of a random source? I mean somethin like we have now in
FEATURE_REQUIRED = periph_hwrng.
@mtausig the randomness source would be chosen by the board's configuration as
I already prepared it for the boards it applies for. We can make this
configurable, but I don't see the use case right now.

You have misunderstood me. I do not want to make the random source configurable,
I would just like to know if it is there respectively require that it is there
upon building my application. 
Just the same thing that happens now, if I add FEATURE_REQUIRED = periph_hwrng
to the Makefile.

@miri64
Copy link
Member Author

miri64 commented Oct 4, 2016

You have misunderstood me. I do not want to make the random source configurable,
I would just like to know if it is there respectively require that it is there
upon building my application.
Just the same thing that happens now, if I add FEATURE_REQUIRED = periph_hwrng
to the Makefile.

I both misunderstood and was confused about the context, because I actually thought we talked about periph_hwnrg ^^". Yes, having something like that might be a good idea (though the board normally defaults to "/dev/zero" in case really no randomness source is available)!

@mtausig
Copy link
Contributor

mtausig commented Oct 5, 2016

(though the board normally defaults to "/dev/zero" in case really no randomness source is available)!

I've seen that. But for security relevant code, I'd rather not use random numbers at all, before using /dev/zero as an entropy source.

@miri64
Copy link
Member Author

miri64 commented Oct 17, 2016

Fixes #2264

hwrng_read(buf, (unsigned int)num);
#elif DEV_RANDOM_SRC == DEV_RANDOM_SRC_PERIPH_ADC
for (unsigned i = 0; i < num * 8; i++) {
bf_set(buf, adc_sample(DEV_RANDOM_ADC, ADC_RES_16BIT) & 0x1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you tested this code? In my understanding it sets the second bit of the first byte of the buffer. More than one byte isn't written to buffer and when my buffer content is 0xff i get back 0xff.

Please test your results http://www.fdk.com/cyber-e/pdf/HM-RAE103.pdf there is a command "rngtest" available in many Linux distribution.

I have tested reading a floating pin on a nRF52 and nRF51. On a Floating nRF52 pin the FIPS test was successful. On a nRF51 i have a 100% fail of FIPS 140-2. For inspiration take a look at https://github.com/infomaniac50/Random/blob/master/Random.cpp

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of bf_set I think its better use XOR here. So it doesn't matter what value are initially stored. You can do multiple ADC reads for a single bit to compensate bad randomness.

Copy link
Member Author

Choose a reason for hiding this comment

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

No I wasn't able to test, yet. I don't have the hardware for that :-/

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed

#elif DEV_RANDOM_SRC == DEV_RANDOM_SRC_AT86RF2XX
at86rf2xx_get_random(dev_random_at86rf2xx, buf, num);
#else
memset(buf, 0, num);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is very dangerous. What is, when someone using this random function without reviewing all code line by line on a platform without HWRNG and ADC for AES key generation in a commercial product?

Whats about letting compilation fail?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I wanted to change this.

@d00616
Copy link
Contributor

d00616 commented Oct 17, 2016

When using ADC as source, this code needs a check if the data is random. Otherwise if someone has access to a Board he can force generating 0 as random data by connecting the RNG pin to GND. This can used to easily attack cryptographic operations.

@kaspar030
Copy link
Contributor

@miri64 I think we need to get back to the drawing board. This PR's solution leaves some important things open.

@d00616
Copy link
Contributor

d00616 commented Oct 17, 2016

I think getting random data should use more then one source when no HWRNG is available or it is not trust-able.
One source can ADC with correction but this source can manipulated.
Another source can HAVEGE: HArdware Volatile Entropy Gathering and Expansion. There are some implementations. https://tls.mbed.org/api/havege_8h.html
There are other sources like timing of things or interrupts, radio....

@miri64
Copy link
Member Author

miri64 commented Oct 18, 2016

@miri64 I think we need to get back to the drawing board. This PR's solution leaves some important things open.

Well, at least we are getting there... ;-)

@miri64
Copy link
Member Author

miri64 commented Oct 19, 2016

I won't find time to fix the issues in this PR for the release.

@miri64 miri64 modified the milestones: Release 2016.10, Release 2017.01 Oct 19, 2016
@miri64
Copy link
Member Author

miri64 commented Nov 29, 2016

Improved on ADC implementation and also removed NULL randomness source. Problem now is that the build will fail, if there is no device supplied.

@miri64
Copy link
Member Author

miri64 commented Jan 13, 2017

Okay, after thinking about it (and watching a lot of talks about randomness at 33C3), I agree with @kaspar030: back to the drawing board.

@miri64 miri64 closed this Jan 13, 2017
@miri64 miri64 deleted the dev_random/api/initial branch January 17, 2017 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Community: Hack'n'ACK candidate This PR is a candidate for review and discussion during one of RIOT's monthly Hack'n'ACK parties Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants