Teensy3.5/3.6: Use TRNG for random seed (#859)

This commit is contained in:
Olivier
2017-05-23 20:37:12 +02:00
committed by GitHub
parent b5595e870d
commit 97ed21ef3b
3 changed files with 36 additions and 13 deletions
+5 -10
View File
@@ -51,18 +51,13 @@ uint8_t hwReadConfig(int adr);
*/
//#define MY_HW_HAS_GETRANDOM
/**
* Sleep for a defined time, using minimum power.
* @param ms Time to sleep, in [ms].
* @return Nonsense, please ignore.
*/
/// @brief unique ID
typedef uint8_t unique_id_t[16];
/**
* Sleep for a defined time, using minimum power.
* @param ms Time to sleep, in [ms].
* @return -1.
* @return MY_WAKE_UP_BY_TIMER.
*/
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 mode Interrupt mode, as passed to attachInterrupt.
* @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);
@@ -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 mode2 Interrupt2 mode, as passed to attachInterrupt.
* @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,
unsigned long ms);
/**
* Retrieve unique ID
* @param uniqueID uniqueID
* @param uniqueID unique ID
* @return True if unique ID successfully retrieved
*/
bool hwUniqueID(unique_id_t* uniqueID);
+18
View File
@@ -132,6 +132,24 @@ uint16_t hwFreeMem()
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, ...)
{
char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
+13 -3
View File
@@ -38,15 +38,25 @@
#define _BV(x) (1<<(x))
#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 hwDigitalWrite(__pin, __value) digitalWriteFast(__pin, __value)
#define hwDigitalRead(__pin) digitalReadFast(__pin)
#define hwPinMode(__pin, __value) pinMode(__pin, __value)
#define hwMillis() millis()
// TODO: use hardware random number generator if Teensy 3.5 or 3.6
#define hwRandomNumberInit() randomSeed(analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN))
void hwRandomNumberInit(void);
bool hwInit(void);
void hwWatchdogReset(void);
void hwReboot(void);