Enhance randomness (AVR, ESP8266, Linux, NRF5) (#845)
This commit changes the way random numbers are available with MySensors. The AVR platform uses a 32 Bit random seed build from a floating analog pin. The way of the generation of this seed is FIPS 140-2 compliant. For support MCU with a fast way to generate random data, the hwGetentropy() command is now available to allow rapid generation of random data. The ESP8266 platform uses the internal random source for randomness for hwGetentropy() and Arduino's random() function. The NRF5 platform initializes the pseudo random number generator from internal hardware number generator. The internal AES-ECB unit generates random data after filling with HWRNG data by AES-CBC. On Linux getrandom() is implemented via syscall(). signerAtsha204SoftGetNonce speed: AVR = ~14000us (sha204Soft) NRF51 = ~152us (AES-CBC), 1739us (old sha204Soft) NRF52 = ~31us (AES-CBC), 183us (old sha204Soft) ESP8266 = ~20us (RANDOM_REG32), 380us (old sha204Soft) All Nonce results tested for FIPS 140-2 compliance successfully.
This commit is contained in:
@@ -84,6 +84,9 @@ bool hwInit()
|
||||
return true;
|
||||
}
|
||||
|
||||
static nrf_ecb_t hwRngData;
|
||||
static int8_t hwRndDataReadPos = -1;
|
||||
|
||||
void hwRandomNumberInit()
|
||||
{
|
||||
// Start HWRNG
|
||||
@@ -106,6 +109,17 @@ void hwRandomNumberInit()
|
||||
}
|
||||
randomSeed(seed);
|
||||
|
||||
// Fill ESB data structure for fast random data generation
|
||||
uint8_t *ecbstruct = (uint8_t *)&hwRngData;
|
||||
for (uint8_t i = 0; i<sizeof(hwRngData); i++) {
|
||||
while (NRF_RNG->EVENTS_VALRDY == 0) {
|
||||
yield();
|
||||
}
|
||||
ecbstruct[i] = NRF_RNG->VALUE;
|
||||
NRF_RNG->EVENTS_VALRDY = 0;
|
||||
}
|
||||
hwRndDataReadPos = 0;
|
||||
|
||||
// Stop HWRNG
|
||||
NRF_RNG->TASKS_STOP = 1;
|
||||
#ifdef NRF51
|
||||
@@ -113,6 +127,55 @@ void hwRandomNumberInit()
|
||||
#endif
|
||||
}
|
||||
|
||||
ssize_t hwGetentropy(void *__buffer, size_t __length)
|
||||
{
|
||||
if (hwRndDataReadPos<0) {
|
||||
// Not initialized
|
||||
hwRandomNumberInit();
|
||||
}
|
||||
|
||||
// cut length if > 256
|
||||
if (__length > 256) {
|
||||
__length = 256;
|
||||
}
|
||||
uint8_t *dst = (uint8_t *)__buffer;
|
||||
|
||||
// Start random number generator
|
||||
for (size_t i = 0; i < __length; i++) {
|
||||
dst[i] = hwRngData.ciphertext[hwRndDataReadPos & 0xfu];
|
||||
MY_CRITICAL_SECTION {
|
||||
if (hwRndDataReadPos >= ((int8_t)sizeof(hwRngData.ciphertext)-1))
|
||||
{
|
||||
// Retry until no error
|
||||
bool need_data = true;
|
||||
while (need_data) {
|
||||
// Stop if another task is running
|
||||
NRF_ECB->TASKS_STOPECB = 1;
|
||||
NRF_ECB->EVENTS_ERRORECB = 0;
|
||||
NRF_ECB->EVENTS_ENDECB = 0;
|
||||
uint32_t ptrbackup = NRF_ECB->ECBDATAPTR;
|
||||
NRF_ECB->ECBDATAPTR = (uint32_t)&hwRngData;
|
||||
NRF_ECB->TASKS_STARTECB = 1;
|
||||
while (!NRF_ECB->EVENTS_ENDECB);
|
||||
NRF_ECB->ECBDATAPTR = ptrbackup;
|
||||
if (NRF_ECB->EVENTS_ERRORECB == 0) {
|
||||
need_data = false;
|
||||
}
|
||||
}
|
||||
hwRndDataReadPos=0;
|
||||
for (uint8_t i=0; i<sizeof(hwRngData.ciphertext); i++) {
|
||||
hwRngData.cleartext[i] ^= hwRngData.ciphertext[i];
|
||||
}
|
||||
} else
|
||||
{
|
||||
hwRndDataReadPos++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return __length;
|
||||
}
|
||||
|
||||
void hwWatchdogReset()
|
||||
{
|
||||
NRF_WDT->RR[0] = WDT_RR_RR_Reload;
|
||||
|
||||
Reference in New Issue
Block a user