diff --git a/core/MySigningAtsha204Soft.cpp b/core/MySigningAtsha204Soft.cpp index 508ea90..37acff9 100644 --- a/core/MySigningAtsha204Soft.cpp +++ b/core/MySigningAtsha204Soft.cpp @@ -149,9 +149,9 @@ bool signerAtsha204SoftGetNonce(MyMessage &msg) return false; } -#ifdef MY_HW_HAS_GETRANDOM - // Try to get MAX_PAYLOAD (or 32) random bytes - while (hwGetentropy(&_signing_verifying_nonce, MIN(MAX_PAYLOAD, 32)) != MIN(MAX_PAYLOAD, 32)); +#ifdef MY_HW_HAS_GETENTROPY + // 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 diff --git a/examples/SecurityPersonalizer/SecurityPersonalizer.ino b/examples/SecurityPersonalizer/SecurityPersonalizer.ino index acc47c8..0939786 100644 --- a/examples/SecurityPersonalizer/SecurityPersonalizer.ino +++ b/examples/SecurityPersonalizer/SecurityPersonalizer.ino @@ -527,13 +527,15 @@ static void halt(bool success) */ static bool generate_random_data(uint8_t* data, size_t sz) { -#ifdef USE_SOFT_SIGNING +#if defined(USE_SOFT_SIGNING) && ! defined(MY_HW_HAS_GETENTROPY) for (size_t i = 0; i < sz; i++) { data[i] = random(256) ^ micros(); unsigned long enter = hwMillis(); while (hwMillis() - enter < (unsigned long)2); } return true; +#elif defined(USE_SOFT_SIGNING) && defined(MY_HW_HAS_GETENTROPY) + hwGetentropy(&data, sz); #else ret_code = sha204.sha204m_random(tx_buffer, rx_buffer, RANDOM_SEED_UPDATE); if ((ret_code != SHA204_SUCCESS) || (lockConfig != 0x00)) { diff --git a/hal/architecture/MyHw.h b/hal/architecture/MyHw.h index 3288ff9..7677500 100644 --- a/hal/architecture/MyHw.h +++ b/hal/architecture/MyHw.h @@ -44,12 +44,12 @@ uint8_t hwReadConfig(int adr); */ /** - * @def MY_HW_HAS_GETRANDOM + * @def MY_HW_HAS_GETENTROPY * @brief Define this, if hwGetentropy is implemented * * ssize_t hwGetentropy(void *__buffer, size_t __length); */ -//#define MY_HW_HAS_GETRANDOM +//#define MY_HW_HAS_GETENTROPY /// @brief unique ID typedef uint8_t unique_id_t[16]; @@ -141,7 +141,7 @@ void hwDebugPrint(const char *fmt, ... ); */ #ifdef DOXYGEN #define MY_CRITICAL_SECTION -#define MY_HW_HAS_GETRANDOM +#define MY_HW_HAS_GETENTROPY #endif /* DOXYGEN */ #endif // #ifdef MyHw_h diff --git a/hal/architecture/MyHwESP8266.h b/hal/architecture/MyHwESP8266.h index 2019b1d..0890687 100644 --- a/hal/architecture/MyHwESP8266.h +++ b/hal/architecture/MyHwESP8266.h @@ -52,7 +52,7 @@ 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 +//#define MY_HW_HAS_GETENTROPY /** * Restore interrupt state. diff --git a/hal/architecture/MyHwLinuxGeneric.h b/hal/architecture/MyHwLinuxGeneric.h index b2ca0b5..b02e216 100644 --- a/hal/architecture/MyHwLinuxGeneric.h +++ b/hal/architecture/MyHwLinuxGeneric.h @@ -58,7 +58,7 @@ 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 +#define MY_HW_HAS_GETENTROPY inline unsigned long hwMillis(); #ifdef MY_RF24_IRQ_PIN diff --git a/hal/architecture/MyHwNRF5.h b/hal/architecture/MyHwNRF5.h index 2349046..e7c788d 100644 --- a/hal/architecture/MyHwNRF5.h +++ b/hal/architecture/MyHwNRF5.h @@ -63,7 +63,7 @@ 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 +#define MY_HW_HAS_GETENTROPY #ifndef MY_SERIALDEVICE #define MY_SERIALDEVICE Serial diff --git a/hal/architecture/MyHwTeensy3.cpp b/hal/architecture/MyHwTeensy3.cpp index e84c65e..eb5071f 100644 --- a/hal/architecture/MyHwTeensy3.cpp +++ b/hal/architecture/MyHwTeensy3.cpp @@ -132,18 +132,33 @@ uint16_t hwFreeMem() return FUNCTION_NOT_SUPPORTED; } -void hwRandomNumberInit(void) +#if defined(MY_HW_HAS_GETENTROPY) +ssize_t hwGetentropy(void *__buffer, const size_t __length) { -#if defined(__MK64FX512__) || defined(__MK66FX1M0__) - // use HW RNG present on Teensy3.5/3.6 - // init RNG SIM_SCGC6 |= SIM_SCGC6_RNGA; // enable RNG RNG_CR &= ~RNG_CR_SLP_MASK; - RNG_CR |= RNG_CR_HA_MASK; // high assurance - // get random number - RNG_CR |= RNG_CR_GO_MASK; - while (!(RNG_SR & RNG_SR_OREG_LVL(0xF))); - const uint32_t seed = RNG_OR; + RNG_CR |= RNG_CR_HA_MASK; //high assurance, not needed + size_t pos = 0; + while (pos < __length) { + RNG_CR |= RNG_CR_GO_MASK; + while (!(RNG_SR & RNG_SR_OREG_LVL(0xF))); + const uint32_t rndVar = RNG_OR; + const uint8_t bsize = (__length - pos) > sizeof(rndVar) ? sizeof(rndVar) : (__length - pos); + (void)memcpy((uint8_t*)__buffer + pos, &rndVar, bsize); + pos += bsize; + } + SIM_SCGC6 &= ~SIM_SCGC6_RNGA; // disable RNG + return pos; +} +#endif + +void hwRandomNumberInit(void) +{ +#if defined(MY_HW_HAS_GETENTROPY) + // use HW RNG present on Teensy3.5/3.6 + // init RNG + uint32_t seed = 0; + hwGetentropy(&seed, sizeof(seed)); randomSeed(seed); #else randomSeed(analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN)); @@ -175,4 +190,4 @@ void hwDebugPrint(const char *fmt, ...) #endif va_end(args); MY_SERIALDEVICE.print(fmtBuffer); -} \ No newline at end of file +} diff --git a/hal/architecture/MyHwTeensy3.h b/hal/architecture/MyHwTeensy3.h index 3ff9ebc..990bdaf 100644 --- a/hal/architecture/MyHwTeensy3.h +++ b/hal/architecture/MyHwTeensy3.h @@ -74,6 +74,10 @@ void hwReboot(void); #define MY_SERIALDEVICE Serial #endif +#if defined(__MK64FX512__) || defined(__MK66FX1M0__) +#define MY_HW_HAS_GETENTROPY +#endif + #define MY_CRITICAL_SECTION ATOMIC_BLOCK(ATOMIC_RESTORESTATE) -#endif \ No newline at end of file +#endif diff --git a/hal/architecture/MyMainLinux.cpp b/hal/architecture/MyMainLinux.cpp index 9768923..f67ea2f 100644 --- a/hal/architecture/MyMainLinux.cpp +++ b/hal/architecture/MyMainLinux.cpp @@ -148,11 +148,7 @@ void generate_soft_sign_hmac_key() { uint8_t key[32]; - for (int i = 0; i < 32; i++) { - key[i] = random(256) ^ micros(); - unsigned long enter = hwMillis(); - while (hwMillis() - enter < (unsigned long)2); - } + while (hwGetentropy(&key, sizeof(key)) != sizeof(key)); print_soft_sign_hmac_key(key); @@ -223,11 +219,7 @@ void generate_soft_sign_serial_key() { uint8_t key[9]; - for (int i = 0; i < 9; i++) { - key[i] = random(256) ^ micros(); - unsigned long enter = hwMillis(); - while (hwMillis() - enter < (unsigned long)2); - } + while (hwGetentropy(&key, sizeof(key)) != sizeof(key)); print_soft_sign_serial_key(key); @@ -303,11 +295,7 @@ void generate_aes_key() { uint8_t key[16]; - for (int i = 0; i < 16; i++) { - key[i] = random(256) ^ micros(); - unsigned long enter = hwMillis(); - while (hwMillis() - enter < (unsigned long)2); - } + while (hwGetentropy(&key, sizeof(key)) != sizeof(key)); print_aes_key(key); diff --git a/keywords.txt b/keywords.txt index ebc77f1..741b10f 100644 --- a/keywords.txt +++ b/keywords.txt @@ -90,6 +90,10 @@ MY_NODE_LOCK_FEATURE LITERAL1 MY_NODE_UNLOCK_PIN LITERAL1 MY_NODE_LOCK_COUNTER_MAX LITERAL1 +# Random +MY_HW_HAS_GETENTROPY LITERAL1 +hwGetentropy KEYWORD2 + # SoftSPI MY_SOFTSPI LITERAL1 MY_SOFT_SPI_SCK_PIN LITERAL1