Update more usage of hwGetentropy() (#869)
* Update more usage of hwGetentropy() * Add hwGetentropy() to Teensy 3.5/3.6
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
#endif
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user