Teensy3.5/3.6: Use TRNG for random seed (#859)
This commit is contained in:
+5
-10
@@ -51,18 +51,13 @@ uint8_t hwReadConfig(int adr);
|
|||||||
*/
|
*/
|
||||||
//#define MY_HW_HAS_GETRANDOM
|
//#define MY_HW_HAS_GETRANDOM
|
||||||
|
|
||||||
/**
|
/// @brief unique ID
|
||||||
* Sleep for a defined time, using minimum power.
|
|
||||||
* @param ms Time to sleep, in [ms].
|
|
||||||
* @return Nonsense, please ignore.
|
|
||||||
*/
|
|
||||||
|
|
||||||
typedef uint8_t unique_id_t[16];
|
typedef uint8_t unique_id_t[16];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sleep for a defined time, using minimum power.
|
* Sleep for a defined time, using minimum power.
|
||||||
* @param ms Time to sleep, in [ms].
|
* @param ms Time to sleep, in [ms].
|
||||||
* @return -1.
|
* @return MY_WAKE_UP_BY_TIMER.
|
||||||
*/
|
*/
|
||||||
int8_t hwSleep(unsigned long ms);
|
int8_t hwSleep(unsigned long ms);
|
||||||
|
|
||||||
@@ -71,7 +66,7 @@ int8_t hwSleep(unsigned long ms);
|
|||||||
* @param interrupt Interrupt number, which can wake the mcu from sleep.
|
* @param interrupt Interrupt number, which can wake the mcu from sleep.
|
||||||
* @param mode Interrupt mode, as passed to attachInterrupt.
|
* @param mode Interrupt mode, as passed to attachInterrupt.
|
||||||
* @param ms Time to sleep, in [ms].
|
* @param ms Time to sleep, in [ms].
|
||||||
* @return -1 when woken by timer, or interrupt number when woken by interrupt.
|
* @return MY_WAKE_UP_BY_TIMER when woken by timer, or interrupt number when woken by interrupt.
|
||||||
*/
|
*/
|
||||||
int8_t hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms);
|
int8_t hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms);
|
||||||
|
|
||||||
@@ -82,14 +77,14 @@ int8_t hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms);
|
|||||||
* @param interrupt2 Interrupt2 number, which can wake the mcu from sleep.
|
* @param interrupt2 Interrupt2 number, which can wake the mcu from sleep.
|
||||||
* @param mode2 Interrupt2 mode, as passed to attachInterrupt.
|
* @param mode2 Interrupt2 mode, as passed to attachInterrupt.
|
||||||
* @param ms Time to sleep, in [ms].
|
* @param ms Time to sleep, in [ms].
|
||||||
* @return -1 when woken by timer, or interrupt number when woken by interrupt.
|
* @return MY_WAKE_UP_BY_TIMER when woken by timer, or interrupt number when woken by interrupt.
|
||||||
*/
|
*/
|
||||||
int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2,
|
int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2,
|
||||||
unsigned long ms);
|
unsigned long ms);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve unique ID
|
* Retrieve unique ID
|
||||||
* @param uniqueID uniqueID
|
* @param uniqueID unique ID
|
||||||
* @return True if unique ID successfully retrieved
|
* @return True if unique ID successfully retrieved
|
||||||
*/
|
*/
|
||||||
bool hwUniqueID(unique_id_t* uniqueID);
|
bool hwUniqueID(unique_id_t* uniqueID);
|
||||||
|
|||||||
@@ -132,6 +132,24 @@ uint16_t hwFreeMem()
|
|||||||
return FUNCTION_NOT_SUPPORTED;
|
return FUNCTION_NOT_SUPPORTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void hwRandomNumberInit(void)
|
||||||
|
{
|
||||||
|
#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;
|
||||||
|
randomSeed(seed);
|
||||||
|
#else
|
||||||
|
randomSeed(analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void hwDebugPrint(const char *fmt, ...)
|
void hwDebugPrint(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
|
char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
|
||||||
|
|||||||
@@ -38,15 +38,25 @@
|
|||||||
#define _BV(x) (1<<(x))
|
#define _BV(x) (1<<(x))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)
|
||||||
|
#define RNG_CR_GO_MASK 0x1u
|
||||||
|
#define RNG_CR_HA_MASK 0x2u
|
||||||
|
#define RNG_CR_INTM_MASK 0x4u
|
||||||
|
#define RNG_CR_CLRI_MASK 0x8u
|
||||||
|
#define RNG_CR_SLP_MASK 0x10u
|
||||||
|
#define RNG_SR_OREG_LVL_MASK 0xFF00u
|
||||||
|
#define RNG_SR_OREG_LVL_SHIFT 8
|
||||||
|
#define RNG_SR_OREG_LVL(x) (((uint32_t)(((uint32_t)(x))<<RNG_SR_OREG_LVL_SHIFT))&RNG_SR_OREG_LVL_MASK)
|
||||||
|
#define SIM_SCGC6_RNGA ((uint32_t)0x00000200)
|
||||||
|
#endif
|
||||||
|
|
||||||
// Define these as macros to save valuable space
|
// Define these as macros to save valuable space
|
||||||
#define hwDigitalWrite(__pin, __value) digitalWriteFast(__pin, __value)
|
#define hwDigitalWrite(__pin, __value) digitalWriteFast(__pin, __value)
|
||||||
#define hwDigitalRead(__pin) digitalReadFast(__pin)
|
#define hwDigitalRead(__pin) digitalReadFast(__pin)
|
||||||
#define hwPinMode(__pin, __value) pinMode(__pin, __value)
|
#define hwPinMode(__pin, __value) pinMode(__pin, __value)
|
||||||
#define hwMillis() millis()
|
#define hwMillis() millis()
|
||||||
|
|
||||||
// TODO: use hardware random number generator if Teensy 3.5 or 3.6
|
void hwRandomNumberInit(void);
|
||||||
#define hwRandomNumberInit() randomSeed(analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN))
|
|
||||||
|
|
||||||
bool hwInit(void);
|
bool hwInit(void);
|
||||||
void hwWatchdogReset(void);
|
void hwWatchdogReset(void);
|
||||||
void hwReboot(void);
|
void hwReboot(void);
|
||||||
|
|||||||
Reference in New Issue
Block a user