Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to disable RSSI-based random number generator in radio.c #621

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lmic/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_*
Expand Down
14 changes: 8 additions & 6 deletions src/lmic/oslmic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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);

Expand Down Expand Up @@ -222,26 +220,30 @@ 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
//! Read 32-bit quantity from given pointer in big endian byte order.
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
//! Read 16-bit quantity from given pointer in little endian byte order.
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()))
Expand Down
10 changes: 10 additions & 0 deletions src/lmic/radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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
Expand All @@ -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 () {
Expand All @@ -1170,6 +1179,7 @@ u1_t radio_rand1 () {
randbuf[0] = i;
return v;
}
#endif

u1_t radio_rssi () {
u1_t r = readReg(LORARegRssiValue);
Expand Down