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:
d00616
2017-05-21 14:12:09 +02:00
committed by Olivier
parent b479a0cf3f
commit 6c12dddf5a
11 changed files with 167 additions and 5 deletions
+5
View File
@@ -109,6 +109,10 @@ bool signerAtsha204SoftGetNonce(MyMessage &msg)
}
SIGN_DEBUG(PSTR("Signing backend: ATSHA204Soft\n"));
#ifdef MY_HW_HAS_GETRANDOM
// Try to get MAX_PAYLOAD random bytes
while (hwGetentropy(&_signing_verifying_nonce, MAX_PAYLOAD) != MAX_PAYLOAD);
#else
// We used a basic whitening technique that XORs a random byte with the current hwMillis() counter and then the byte is
// hashed (SHA256) to produce the resulting nonce
_signing_sha256.init();
@@ -116,6 +120,7 @@ bool signerAtsha204SoftGetNonce(MyMessage &msg)
_signing_sha256.write(random(256) ^ (hwMillis()&0xFF));
}
memcpy(_signing_verifying_nonce, _signing_sha256.result(), MAX_PAYLOAD);
#endif
// We set the part of the 32-byte nonce that does not fit into a message to 0xAA
memset(&_signing_verifying_nonce[MAX_PAYLOAD], 0xAA, sizeof(_signing_verifying_nonce)-MAX_PAYLOAD);
@@ -72,7 +72,7 @@
*
* @b Important<br>
* You will need to ensure @ref MY_SIGNING_SOFT_RANDOMSEED_PIN is set to an unconnected analog pin
* in order to provide entropy to the software RNG.
* in order to provide entropy to the software RNG if your hardware has no HWRNG.
*
* @note The generated keys displayed in the serial log with this setting needs to be written down
* and transferred to all nodes this gateway will communicate with. This is mandatory for ALL
@@ -436,7 +436,7 @@ void setup()
while (hwMillis() - enter < (unsigned long)500);
#ifdef USE_SOFT_SIGNING
// initialize pseudo-RNG
randomSeed(analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN));
hwRandomNumberInit();
#endif
Serial.begin(MY_BAUD_RATE);
+9
View File
@@ -43,6 +43,14 @@ void hwWriteConfig(int adr, uint8_t value);
uint8_t hwReadConfig(int adr);
*/
/**
* @def MY_HW_HAS_GETRANDOM
* @brief Define this, if hwGetentropy is implemented
*
* ssize_t hwGetentropy(void *__buffer, size_t __length);
*/
//#define MY_HW_HAS_GETRANDOM
/**
* Sleep for a defined time, using minimum power.
* @param ms Time to sleep, in [ms].
@@ -138,6 +146,7 @@ void hwDebugPrint(const char *fmt, ... );
*/
#ifdef DOXYGEN
#define MY_CRITICAL_SECTION
#define MY_HW_HAS_GETRANDOM
#endif /* DOXYGEN */
#endif // #ifdef MyHw_h
+35
View File
@@ -202,6 +202,41 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
return ret;
}
inline void hwRandomNumberInit()
{
// This function initializes the random number generator with a seed
// of 32 bits. This method is good enough to earn FIPS 140-2 conform
// random data. This should reach to generate 32 Bit for randomSeed().
unsigned long seed = 0;
unsigned long start = millis();
unsigned long timeout = start + 20;
// Trigger floating effect of an unconnected pin
pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT_PULLUP);
pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT);
delay(10);
// Generate 32 bits of datas
for (uint8_t i=0; i<32; i++) {
int pinValue = analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN);
// Wait until the analog value has changed
while ((pinValue == analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN)) && (timeout>=millis())) {
seed ^= (millis() << i);
// Check of data generation is slow
if (timeout<=millis()) {
// Trigger pin again
pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT_PULLUP);
pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT);
// Pause a short while
delay(seed % 10);
timeout = millis()+20;
}
}
}
randomSeed(seed);
}
bool hwUniqueID(unique_id_t* uniqueID)
{
// not implemented yet
+1 -1
View File
@@ -48,12 +48,12 @@ bool hwInit(void);
#define hwWatchdogReset() wdt_reset()
#define hwReboot() wdt_enable(WDTO_15MS); while (1)
#define hwMillis() millis()
#define hwRandomNumberInit() randomSeed(analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN))
#define hwReadConfig(__pos) eeprom_read_byte((uint8_t*)(__pos))
#define hwWriteConfig(__pos, __val) eeprom_update_byte((uint8_t*)(__pos), (__val))
#define hwReadConfigBlock(__buf, __pos, __length) eeprom_read_block((void*)(__buf), (void*)(__pos), (__length))
#define hwWriteConfigBlock(__buf, __pos, __length) eeprom_update_block((void*)(__buf), (void*)(__pos), (__length))
inline void hwRandomNumberInit();
void hwInternalSleep(unsigned long ms);
#ifndef DOXYGEN
+15
View File
@@ -73,6 +73,21 @@ bool hwUniqueID(unique_id_t *uniqueID)
return true;
}
ssize_t hwGetentropy(void *__buffer, size_t __length)
{
// 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] = (uint8_t)RANDOM_REG32;
}
return __length;
}
int8_t hwSleep(unsigned long ms)
{
+4 -1
View File
@@ -41,13 +41,16 @@
#define hwWatchdogReset() wdt_reset()
#define hwReboot() ESP.restart()
#define hwMillis() millis()
#define hwRandomNumberInit() randomSeed(RANDOM_REG32)
// The use of randomSeed switch to pseudo random number. Keep hwRandomNumberInit empty
#define hwRandomNumberInit()
bool hwInit(void);
void hwReadConfigBlock(void* buf, void* adr, size_t length);
void hwWriteConfigBlock(void* buf, void* adr, size_t length);
void hwWriteConfig(const int addr, uint8_t value);
uint8_t hwReadConfig(const int addr);
ssize_t hwGetentropy(void *__buffer, size_t __length);
//#define MY_HW_HAS_GETRANDOM
/**
* Restore interrupt state.
+15 -1
View File
@@ -19,7 +19,13 @@
#include "MyHwLinuxGeneric.h"
#include <errno.h>
#include <linux/random.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <syscall.h>
#include <unistd.h>
#include <time.h>
#include "SoftEeprom.h"
#include "log.h"
@@ -62,7 +68,15 @@ void hwWriteConfig(int addr, uint8_t value)
void hwRandomNumberInit()
{
randomSeed(time(NULL));
unsigned long seed=0;
while (hwGetentropy(&seed, sizeof(seed)) != sizeof(seed));
randomSeed(seed);
}
ssize_t hwGetentropy(void *__buffer, size_t __length)
{
// getrandom syscall
return syscall(SYS_getrandom, __buffer, __length, GRND_NONBLOCK);
}
unsigned long hwMillis()
+2
View File
@@ -54,6 +54,8 @@ inline void hwWriteConfigBlock(void* buf, void* addr, size_t length);
inline uint8_t hwReadConfig(int addr);
inline void hwWriteConfig(int addr, uint8_t value);
inline void hwRandomNumberInit();
ssize_t hwGetentropy(void *__buffer, size_t __length);
#define MY_HW_HAS_GETRANDOM
inline unsigned long hwMillis();
#ifdef MY_RF24_IRQ_PIN
+63
View File
@@ -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;
+16
View File
@@ -59,6 +59,8 @@ void hwWriteConfigBlock(void *buf, void *adr, size_t length);
void hwWriteConfig(int adr, uint8_t value);
uint8_t hwReadConfig(int adr);
void hwRandomNumberInit();
ssize_t hwGetentropy(void *__buffer, size_t __length);
#define MY_HW_HAS_GETRANDOM
#ifndef MY_SERIALDEVICE
#define MY_SERIALDEVICE Serial
@@ -110,6 +112,20 @@ static __inline__ void __priMaskRestore(const uint32_t *priMask)
#define MY_HW_RTC_IRQN RTC0_IRQn
#endif
/** Datastructure for AES ECB unit
*/
typedef struct {
/** AES Key
*/
uint8_t key[16];
/** Unencrypted data
*/
uint8_t cleartext[16];
/** Encrypted data
*/
uint8_t ciphertext[16];
} nrf_ecb_t;
#ifndef DOXYGEN
#define MY_CRITICAL_SECTION
// temp. commented, to fix "CircularBuffer.h:94:2: warning: control reaches end of non-void function" error