pico-sdk icon indicating copy to clipboard operation
pico-sdk copied to clipboard

API to support generate random number?

Open xiongyu0523 opened this issue 4 years ago • 9 comments

is there a plan to add hardware random number generation?

xiongyu0523 avatar Sep 21 '21 03:09 xiongyu0523

Are you looking for "2.17.5. Random Number Generator" in https://datasheets.raspberrypi.org/rp2040/rp2040-datasheet.pdf ? Or are you asking for a "nice API" to wrap that?

lurch avatar Sep 21 '21 08:09 lurch

You can read the ring oscillator random bit with something like

#include "hardware/structs/rosc.h"

bool get_random_bit() {
    return rosc_hw->randombit;
}

This is just an unconditioned ring oscillator sample. It's not well characterised, somewhat biased, output is somewhat periodic when sampled rapidly, etc. We haven't wrapped it up in a simplified API because that might give an impression of randomness guarantees that we haven't done the characterisation to back up. It's very much "some assembly required".

What functionality are you looking for exactly?

Wren6991 avatar Sep 21 '21 14:09 Wren6991

See also the discussion about randomness on the forums.

lurch avatar Sep 21 '21 14:09 lurch

Thank you very much for your example and forum discussions. I will use the rand number for the SSL library port. I can understand the hardware characteristic is not defined yet, but providing such a API is still useful and can be a seed feed to srand

xiongyu0523 avatar Sep 24 '21 14:09 xiongyu0523

~~This is fixed thanks to #4213. You can generate randomness on the RP2040 using os.urandom .~~

makew0rld avatar Mar 21 '22 00:03 makew0rld

@makeworld-the-better-one It sounds like you might be mixing up the RP2040 C SDK with the RP2040 MicroPython port?

lurch avatar Mar 21 '22 09:03 lurch

Yep sorry, my bad

makew0rld avatar Mar 21 '22 13:03 makew0rld

This seems like a useful addition, as the random bit is not sufficiently random if polled at high frequency

kilograham avatar Jul 14 '22 18:07 kilograham

Thank you very much for your example and forum discussions. I will use the rand number for the SSL library port. I can understand the hardware characteristic is not defined yet, but providing such a API is still useful and can be a seed feed to srand

You did see the note in the forums? "The datsheet does say that the randomness is not suitable for cryptography, since it can be compromised, and thus is only suitable for less critical applications."

cameronelliott avatar Sep 02 '22 07:09 cameronelliott

fixed by #1111

kilograham avatar Jan 20 '23 21:01 kilograham

merged into develop

kilograham avatar Jan 27 '23 14:01 kilograham