Random number generator sample code

Learn MoreFAQsTutorials

2 posts / 0 new
Last post
Ankit
Offline
Last seen:8 months 2 weeks ago
加入:2017-05-24 07:42
Random number generator sample code

I use trng_acquire( uint8_t* rnd_no ) function. I am not able to get random number every time. Below is my sample code.

空白aes_test(空白)
{
uint8_t random_number[16];

trng_acquire( random_number ); // random number received successfully
memcpy(aes_env.aes_key.iv, IV, 16);
rwip_schedule();
trng_acquire( random_number ); // random number received successfully
aes_init(false, NULL );
aes_operation(key, sizeof(key), Plaintext, sizeof(Plaintext), aes_out, sizeof(aes_out), 0, NULL, 0);
rwip_schedule();
trng_acquire( random_number ); // cannot receive random number
aes_operation(key, sizeof(key), aes_out, sizeof(Plaintext), aes_result, sizeof(aes_out), 1, aes_done_cb, 0);
rwip_schedule();
trng_acquire( random_number ); // cannot receive random number
}

当我调试上面鳕鱼e, I am able to generate random number for first two times. In next two trial I receive no random number. Means random_number variable is not upgraded.

So, now i use rand() function and it is working fine.

But still I want to know that, in above example, why am I not getting new random number every time.

Device:
MT_dialog
Offline
Last seen:2 weeks 4 days ago
Staff
加入:2015-06-08 11:34
Hi Ankit,

Hi Ankit,

The purpose of the trng_aquire() is only to seed the random function and not be used directly, the reason you cannot use the trng_aquire() directly is the fact that in order to get from the 580 a random number the SDK uses the radio module and by altering the values of the radio registers it obtains the random number, so as you can imagine doing that over and over again isn't suggested, since if a BLE interrupt occurs while the radio is in an undefined state then this will have unexpected results. The proper use is via the rand() function. Now regarding the values are not updated during debugging i suppose that this has to do with the fact that you are in debugging mode since i tried to repeatedly invoke multiple time the trng_aquire() but i always get a new generated number value and printing it. The problem occurs when trying to have a BLE operation (advertising for example) and run the trng_aquire() function that as mentioned will have unexpected results and at least on my side i got always zeros when for a random value and couldn't find the BLE device. So to sum up, dont use the trng_aquire() function in order to generate random data, but the rand() function.

Thanks MT_dialog