diff --git a/src/lmic/config.h b/src/lmic/config.h index 8673b46a..dfc9de92 100644 --- a/src/lmic/config.h +++ b/src/lmic/config.h @@ -151,6 +151,14 @@ # error "You may define at most one of USE_ORIGINAL_AES and USE_IDEETRON_AES" #endif + +// By default LMIC use the radio to gather random seed from wideband RSSI +// measurements. Extracting good quality random seed takes 480 SPI transactions +// on average. This flag allows alternative random implementations, like +// true-RNGs built-in to many MCUs. Use in conjuction with os_getRndU1. +// define this in lmic_project_config.h to disable radio.c random number generator +//#define LMIC_DISABLE_RADIO_RAND + // LMIC_DISABLE_DR_LEGACY // turn off legacy DR_* symbols that vary by bandplan. // Older code uses these for configuration. EU868_DR_*, US915_DR_* diff --git a/src/lmic/oslmic.h b/src/lmic/oslmic.h index 448b0290..ae79fc95 100644 --- a/src/lmic/oslmic.h +++ b/src/lmic/oslmic.h @@ -92,9 +92,6 @@ extern u4_t AESKEY[]; #define AESaux ((u1_t*)AESAUX) #define FUNC_ADDR(func) (&(func)) -u1_t radio_rand1 (void); -#define os_getRndU1() radio_rand1() - #define DEFINE_LMIC struct lmic_t LMIC #define DECLARE_LMIC extern struct lmic_t LMIC @@ -114,6 +111,7 @@ void os_init (void); int os_init_ex (const void *pPinMap); void os_runloop (void); void os_runloop_once (void); +u1_t radio_rand1 (void); u1_t radio_rssi (void); void radio_monitor_rssi(ostime_t n, oslmic_radio_rssi_t *pRssi); @@ -222,7 +220,7 @@ bit_t os_queryTimeCriticalJobs(ostime_t time); u4_t os_rlsbf4 (xref2cu1_t buf); #endif #ifndef os_wlsbf4 -//! Write 32-bit quntity into buffer in little endian byte order. +//! Write 32-bit quantity into buffer in little endian byte order. void os_wlsbf4 (xref2u1_t buf, u4_t value); #endif #ifndef os_rmsbf4 @@ -230,7 +228,7 @@ void os_wlsbf4 (xref2u1_t buf, u4_t value); u4_t os_rmsbf4 (xref2cu1_t buf); #endif #ifndef os_wmsbf4 -//! Write 32-bit quntity into buffer in big endian byte order. +//! Write 32-bit quantity into buffer in big endian byte order. void os_wmsbf4 (xref2u1_t buf, u4_t value); #endif #ifndef os_rlsbf2 @@ -238,10 +236,14 @@ void os_wmsbf4 (xref2u1_t buf, u4_t value); u2_t os_rlsbf2 (xref2cu1_t buf); #endif #ifndef os_wlsbf2 -//! Write 16-bit quntity into buffer in little endian byte order. +//! Write 16-bit quantity into buffer in little endian byte order. void os_wlsbf2 (xref2u1_t buf, u2_t value); #endif +//! Get random number (default impl for u1_t). +#ifndef os_getRndU1 +#define os_getRndU1() radio_rand1() +#endif //! Get random number (default impl for u2_t). #ifndef os_getRndU2 #define os_getRndU2() ((u2_t)((os_getRndU1()<<8)|os_getRndU1())) diff --git a/src/lmic/radio.c b/src/lmic/radio.c index d6cc9088..aa9583fb 100644 --- a/src/lmic/radio.c +++ b/src/lmic/radio.c @@ -373,9 +373,12 @@ #error Missing CFG_sx1272_radio/CFG_sx1276_radio #endif + +#if !defined(LMIC_DISABLE_RADIO_RAND) // RADIO STATE // (initialized by radio_init(), used by radio_rand1()) static u1_t randbuf[16]; +#endif static void writeReg (u1_t addr, u1_t data ) { @@ -1117,21 +1120,26 @@ int radio_init () { #else #error Missing CFG_sx1272_radio/CFG_sx1276_radio #endif + // set the tcxo input, if needed if (hal_queryUsingTcxo()) writeReg(RegTcxo, readReg(RegTcxo) | RegTcxo_TcxoInputOn); +#if !defined(LMIC_DISABLE_RADIO_RAND) // seed 15-byte randomness via noise rssi rxlora(RXMODE_RSSI); while( (readReg(RegOpMode) & OPMODE_MASK) != OPMODE_RX ); // continuous rx for(int i=1; i<16; i++) { for(int j=0; j<8; j++) { + // von Neumann extractor: + // creates uniform distribution random from a non-uniformly distributed random source. u1_t b; // wait for two non-identical subsequent least-significant bits while( (b = readReg(LORARegRssiWideband) & 0x01) == (readReg(LORARegRssiWideband) & 0x01) ); randbuf[i] = (randbuf[i] << 1) | b; } } randbuf[0] = 16; // set initial index +#endif #ifdef CFG_sx1276mb1_board // chain calibration @@ -1157,6 +1165,7 @@ int radio_init () { return 1; } +#if !defined(LMIC_DISABLE_RADIO_RAND) // return next random byte derived from seed buffer // (buf[0] holds index of next byte to be returned) u1_t radio_rand1 () { @@ -1170,6 +1179,7 @@ u1_t radio_rand1 () { randbuf[0] = i; return v; } +#endif u1_t radio_rssi () { u1_t r = readReg(LORARegRssiValue);