underverk / SmartWatch

The watch, The hack (just for a while)
Other
64 stars 21 forks source link

TRNG implementation #2

Closed cedeckovec closed 10 years ago

cedeckovec commented 11 years ago

Hi all

ARM M3 mcu support true random generator. Is possible implementation this in smartwatch toolchain ?

Thanks info

underverk commented 11 years ago

Yes it does! If you look in the header files, there is actually a note on how we should implement this. Feel free to implement this and make a pull request and we will incorporate it in the main repository.

cedeckovec commented 11 years ago

Please help im testing this but not working

include "stm32f2xx_rng.h"

int val;

val = RNG_GetRandomNumber();

This code compile without errors but not working.

Many thanks help or example

underverk commented 11 years ago

Have you read the stgm32f2xx_rng.c file? It has alot of information on how to use the RNG peripheral.

First of all, you need: RNG_Cmd(ENABLE);

Then, as with most TRNG, values cannot be instantly calculated - you need to wait for them to become available with something like: while(!RNG_GetFlagStatus(RNG_FLAG_DRDY));

According to the stgm32f2xx_rng.c documenation, you should also check for seed failure (low entropy) using GetFlagStatus on RNG_FLAG_SECS... If it's invalid you need to clear the flag with RNG_ClearFlag and restart the RNG peripheral with RNG_Cmd, and repeat the process. This check IS necessary, as the random nature of the source should, although very seldom, generate the conditions to trigger the seed failure = this can happen under normal circumstances. Else, when this happens, random number generation will stop. All of this is in the stgm32f2xx_rng.c file.

underverk commented 11 years ago

Oh, I forgot... you most likely want to put...

RNG_DeInit(); RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE);

...before you enable the RNG.

The first line resets the peripheral and the second one enables the clock gate for it. This is something you do for virtually all peripherals in the controller, if you want to use them. You'll see similar lines in the _init function of most of the drivers.